View Issue Details

IDProjectCategoryView StatusLast Update
0011697LazarusLCLpublic2019-11-09 12:55
ReporterMartin FriebeAssigned To 
PrioritylowSeveritytweakReproducibilityalways
Status acknowledgedResolutionopen 
Product Version0.9.25 (SVN)Product Build 
Target VersionFixed in Version 
Summary0011697: Lazarus (LCL) throws exception if compiled with -CR (win 32 only)
Description-CR apprently ensures correct typecasting.
But in lcl\interfaces\win32\win32wsforms.pp line 395

  if wcfInitializing in TWinControlAccess(AWinControl).FWinControlFlags then

which is:
type
  TWinControlAccess = class(TWinControl)
  end;

This is simply to bypass encapsulation. Variables in TWinControlAccess are memory-located the same as in TWinControl, and because TWinControlAccess is declared in the current package, the code is suddenly allowed to access protected values...

-CR verifies that AWinControl inherits from TWinControlAccess, which it doesn't.

In this particular case, there is no bug (as in wrong behaviour) behind this. it's more a design issue. But it makes it impossible to test the rest of the LCL.
TagsNo tags attached.
Fixed in Revision
LazTarget-
WidgetsetWin32/Win64
Attached Files

Relationships

has duplicate 0034597 resolvedBart Broersma win32wsbuttons.pp prevents compileroption -CR 
related to 0027759 resolvedJuha Manninen Fix TreeView "nothing selected" index 

Activities

Martin Friebe

2008-07-19 02:02

manager   ~0020806

just seen GTK and wince do this too. (gtkcallback.inc / wincecallback.inc / win32callback.inc)

Phil

2008-07-19 06:07

reporter   ~0020807

Same with -Cr too for Carbon widgetset and maybe others. Hard to find real range check bugs when apparently harmless range checks occur too.

Martin Friebe

2008-07-19 11:17

manager   ~0020810

@Phil
-Cr vs -CR from the docs:
" -CR Generate checks when calling methods to verify if the virtual method table for that object is valid. "

=> It also checks typeasts: TFoo(a) becomes (a as TFoo)

However -CR does include -Cr, if I understand correctly. Windows Lazarus works well with -Cr (only range checks). If Carbon doesn't you should open a separate Bug for this)

-----
@All
I can see 2 potential ways to fix this.

1) Add public methods to TWinControl, that allow reading, setting, removing the flag.
This seems to be bad, as it exposes this ability globally, and it's not meant to be accessible by others (except if you bypass the rules, like lcl does)

2) Find the common BaseClass of all the lcl Classes that need this access. (worst case TObject, but there may be a more specialised)
Insert a new BaseClass: TFooAllWidgetBase
Implement TFooAllWidgetBase in the same package as TWinControl. Now TFooAllWidgetBase is allowed to access the protected properties. Add methods into this baseclass to to the desired work.


If Anyone from the Lazarus Tam wants to take the decision, if such an approach is acceptable... I am happy to write the patch (for this and other occurrences I may find.

Zeljan Rikalo

2010-10-30 12:53

developer   ~0042408

@Martin is this fixed ?

Martin Friebe

2010-10-31 02:24

manager   ~0042469

I can't tell if the originally described issue (at the described position) still exists.

Currently -CR fails here:
(and no, I have not hacked my IDE to use alphablend, it (attempts to set it to false)

#0 RUNERROR(219) at ..\inc\system.inc:985
0000001 FPC_CHECK_OBJECT_EXT(0x0, 0xefa7ac) at ..\inc\generic.inc:853

    TWSCustomFormClass(WidgetSetClass).SetAlphaBlend(Self, AlphaBlend, AlphaBlendValue);
0000002 INITIALIZEWND(0xcee3870) at include\customform.inc:1037
0000003 CREATEWND(0xcee3870) at include\wincontrol.inc:7255
0000004 CREATEWND(0xcee3870) at include\scrollingwincontrol.inc:34
0000005 CREATEWND(0xcee3870) at include\customform.inc:2482
0000006 CREATEWND(0xcee3870) at include\customform.inc:2842
0000007 CREATEHANDLE(0xcee3870) at include\wincontrol.inc:7145
0000008 HANDLENEEDED(0xcee3870) at include\wincontrol.inc:7578
0000009 CREATEFORM(0x14c4b0, 0x1213714, @0x135c9e0: 0) at include\application.inc:2102
0000010 CREATE(0x18c418, 0xe27960, 0x14c4b0) at main.pp:1309
0000011 main at lazarus.pp:102

Zeljan Rikalo

2010-11-29 12:22

developer   ~0043720

@Martin, just rebuilded r28554 (win32) under Win7 with -dDebug (it enables -Cr afaik) and there's no single problem.

Vincent Snijders

2010-11-29 12:36

manager   ~0043723

@Zeljan, this issue is about compiling with -CR (capital R), not -Cr.

errno

2018-11-26 14:20

reporter   ~0112205

Last edited: 2019-11-09 12:55

View 9 revisions

>if wcfInitializing in TWinControlAccess(AWinControl).FWinControlFlags

In my opinion , as AwinControl is not a descendent from TWinControlAccess , even if it works , this line is a hack and a violation of pascal rules and should not be used .
Afaik nothing garentees that it should work .


@Martin Friebe
There is possibly a 3rd easyer solution (and more convenient for ponctuals issues) by using class helper .

example :

TBitBtnAceess could be replaced by

{ TTBitBtnClassHelper }

type TTBitBtnClassHelper=class helper for TBitBtn
private
  function getFButtonGlyph_:TButtonGlyph;
public
  property FButtonGlyph_:TButtonGlyph read getFButtonGlyph_;
end;

 { TTBitBtnClassHelper }

function TTBitBtnClassHelper.getFButtonGlyph_:TButtonGlyph;
begin
 result:=FButtonGlyph;
end;

And thus at line 601 from $(LazarusDir)\lcl\interfaces\win32\win32wsbuttons.pp


TBitBtnAceess(BitBtn).FButtonGlyph.GetImageIndexAndEffect(Low(TButtonState), BitBtn.Font.PixelsPerInch, 1,
        AImageRes, AIndex, AEffect);

should be replaced by

BitBtn.FButtonGlyph_.GetImageIndexAndEffect(Low(TButtonState), BitBtn.Font.PixelsPerInch, 1,
        AImageRes, AIndex, AEffect);



Strangely TWinControlAccess(AWinControl).FWinControlFlags is accepted in my case with fpc3.0.4 with option -CR .

Issue History

Date Modified Username Field Change
2008-07-19 01:59 Martin Friebe New Issue
2008-07-19 01:59 Martin Friebe LazTarget => -
2008-07-19 01:59 Martin Friebe Widgetset => Win32
2008-07-19 02:02 Martin Friebe Note Added: 0020806
2008-07-19 06:07 Phil Note Added: 0020807
2008-07-19 11:17 Martin Friebe Note Added: 0020810
2008-07-20 19:40 Vincent Snijders LazTarget - => post 1.2
2008-07-20 19:40 Vincent Snijders Status new => acknowledged
2010-10-30 12:53 Zeljan Rikalo Note Added: 0042408
2010-10-30 12:53 Zeljan Rikalo Status acknowledged => feedback
2010-10-31 02:24 Martin Friebe Note Added: 0042469
2010-11-29 11:57 Vincent Snijders Status feedback => acknowledged
2010-11-29 12:22 Zeljan Rikalo Note Added: 0043720
2010-11-29 12:36 Vincent Snijders Note Added: 0043723
2013-09-03 12:07 Martin Friebe LazTarget post 1.2 => 1.4
2014-09-10 19:58 Juha Manninen LazTarget 1.4 => -
2015-04-03 11:56 Juha Manninen Relationship added related to 0027759
2018-11-25 12:35 Juha Manninen Relationship added has duplicate 0034597
2018-11-26 14:20 errno Note Added: 0112205
2018-11-26 14:21 errno Note Edited: 0112205 View Revisions
2018-11-26 14:29 errno Note Edited: 0112205 View Revisions
2018-11-26 14:39 errno Note Edited: 0112205 View Revisions
2019-02-22 15:47 errno Note Edited: 0112205 View Revisions
2019-02-22 15:52 errno Note Edited: 0112205 View Revisions
2019-02-22 15:59 errno Note Edited: 0112205 View Revisions
2019-02-22 16:54 errno Note Edited: 0112205 View Revisions
2019-11-09 12:55 errno Note Edited: 0112205 View Revisions