Recent

Author Topic: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance  (Read 6055 times)

Pascal

  • Hero Member
  • *****
  • Posts: 922
[SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« on: February 09, 2015, 10:58:52 am »
Hello,
i have a crash on creating my own SynEdit class.
This does not happen the firtst time but on every run at the same
count of Create.

This is the Debugger window:
Code: [Select]
SYSTEM_TOBJECT_$__NEWINSTANCE$$TOBJECT
0040AF50 83ec08                   sub    $0x8,%esp
0040AF53 895c2404                 mov    %ebx,0x4(%esp)
0040AF57 89c3                     mov    %eax,%ebx
0040AF59 89d8                     mov    %ebx,%eax
0040AF5B 8b10                     mov    (%eax),%edx
0040AF5D 89e0                     mov    %esp,%eax
0040AF5F e88c2c0000               call   0x40dbf0 <SYSTEM_GETMEM$POINTER$LONGWORD>
0040AF64 8b0424                   mov    (%esp),%eax
0040AF67 85c0                     test   %eax,%eax
0040AF69 7428                     je     0x40af93 <SYSTEM_TOBJECT_$__NEWINSTANCE$$TOBJECT+67>
0040AF6B 8b13                     mov    (%ebx),%edx
0040AF6D 8b0424                   mov    (%esp),%eax
0040AF70 b100                     mov    $0x0,%cl
0040AF72 e8a987ffff               call   0x403720 <SYSTEM_FILLCHAR$formal$LONGINT$BYTE>
0040AF77 8b0424                   mov    (%esp),%eax

And this is the call stack:
Code: [Select]
#0 SYSTEM_TOBJECT_$__NEWINSTANCE$$TOBJECT at :0
#1 TSYNEDITLINES__CREATE(0x4390e98, 0x57db80 <TCUSTOMSYNEDIT__MARKTEXTASSAVED>, 0x6647a0, <error reading variable>) at syneditlines.pas:302
#2 TCUSTOMSYNEDIT__CREATE(0x4390e98, 0x6647a0, <error reading variable>) at synedit.pp:1922
#3 TSUSEDIT__CREATE(0x0, 0xe29cd68 'C:\X-Lazarus\Projekte\susIDE\Cobol\h054backup\src\SFCF8360.CBL', 0x1cbca8 'C:\X-Lazarus\Projekte\susIDE\Cobol\h054backup\src\SFPB8360.CBL', 0x601404, <error reading variable>) at susedit.pas:138
#4 TEDITORFILE__CREATEFORCOPY(0x1e95acc0 'SFCF8360.CBL', <error reading variable>) at susEditorFile.pas:864
#5 WORTSPEICHERN(0x2aef784) at susEditorFile.pas:726
#6 TEDITORFILE__SCANEDITORFILE(0, 0x1e2250, 0x1b8c78, <error reading variable>) at susEditorFile.pas:804
#7 TEDITORFILE__SCAN(<error reading variable>) at susEditorFile.pas:327
#8 TFORMMAIN__LOADFILE(0x43894d8 'C:\X-Lazarus\Projekte\susIDE\Cobol\h054backup\src\SFPB8360.CBL', <error reading variable>) at susMain.pas:446
#9 TFORMMAIN__MENUITEM_OEFFNENCLICK(0x43b02f0, <error reading variable>) at susMain.pas:222
#10 TMENUITEM__CLICK(<error reading variable>) at .\include\menuitem.inc:84
#11 HANDLEITEM(0x43b02f0, 0x2aef900) at .\include\menu.inc:247
#12 TMENU__ISSHORTCUT({MSG = 48384, CHARCODE = 79, UNUSED = 62768, KEYDATA = 1572865, RESULT = 0}, <error reading variable>) at .\include\menu.inc:266
#13 TCUSTOMFORM__ISSHORTCUT({MSG = 48384, CHARCODE = 79, UNUSED = 62768, KEYDATA = 1572865, RESULT = 0}, <error reading variable>) at .\include\customform.inc:2473
#14 ISSHORTCUT(0x2aef960) at .\include\wincontrol.inc:5645
#15 TWINCONTROL__DOKEYDOWNBEFOREINTERFACE({MSG = 48384, CHARCODE = 79, UNUSED = 62768, KEYDATA = 1572865, RESULT = 0}, false, <error reading variable>) at .\include\wincontrol.inc:5708
#16 TWINCONTROL__CNKEYDOWN({MSG = 48384, CHARCODE = 79, UNUSED = 62768, KEYDATA = 1572865, RESULT = 0}, <error reading variable>) at .\include\wincontrol.inc:7006
#17 SYSTEM_TOBJECT_$__DISPATCH$formal at :0
#18 RAISELOOP(0x2aefc14) at .\include\wincontrol.inc:6916
#19 TWINCONTROL__WNDPROC({MSG = 48384, WPARAM = -181403569, LPARAM = 1572865, RESULT = 0, WPARAMLO = 79, WPARAMHI = 62768, WPARAMFILLER = {}, LPARAMLO = 1, LPARAMHI = 24, LPARAMFILLER = {}, RESULTLO = 0, RESULTHI = 0, RESULTFILLER = {}}, <error reading variable>) at .\include\wincontrol.inc:5326
#20 DELIVERMESSAGE(0x1e1340, void) at lclmessageglue.pas:112
#21 WINDOWPROC(461706, 256, 79, 1572865) at .\win32\win32callback.inc:2480
#22 USER32!CallWindowProcA at :0
#23 USER32!CallWindowProcA at :0
#24 USER32!GetOpenClipboardWindow at :0
#25 USER32!DlgDirListComboBoxW at :0
#26 TWIN32WIDGETSET__APPPROCESSMESSAGES(<error reading variable>) at .\win32\win32object.inc:367
#27 TAPPLICATION__HANDLEMESSAGE(<error reading variable>) at .\include\application.inc:1272
#28 TAPPLICATION__RUNLOOP(<error reading variable>) at .\include\application.inc:1405
#29 TWIDGETSET__APPRUN(0x422e50 <TAPPLICATION__RUNLOOP>, <error reading variable>) at .\include\interfacebase.inc:54
#30 TAPPLICATION__RUN(<error reading variable>) at .\include\application.inc:1393
#31 main at susIDE.lpr:19

At the stage of the error only the standard constructors where called:
Code: [Select]
constructor TsusEdit.Create(AOwner: TComponent; AFileName, AEditorFile: String);
begin
  inherited Create(AOwner); <--
Code: [Select]
constructor TCustomSynEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  SetInline(True);
  ControlStyle:=ControlStyle+[csOwnedChildrenNotSelectable];
  FScrollBarUpdateLock := 0;
  FPaintLock := 0;
  FStatusChangeLock := 0;
  FUndoBlockAtPaintLock := 0;
  FRecalcCharsAndLinesLock := 0;

  FStatusChangedList := TSynStatusChangedHandlerList.Create;

  FDefaultBeautifier := TSynBeautifier.Create(self);
  FBeautifier := FDefaultBeautifier;

  FLines := TSynEditStringList.Create;
  TSynEditStringList(FLines).AttachSynEdit(Self);

  FCaret := TSynEditCaret.Create;
  FCaret.MaxLeftChar := @CurrentMaxLineLen;
  FCaret.AddChangeHandler(@CaretChanged);
  FInternalCaret := TSynEditCaret.Create;
  FInternalCaret.MaxLeftChar := @CurrentMaxLineLen;

  // Create the lines/views
  FTrimmedLinesView := TSynEditStringTrimmingList.Create(fLines, fCaret);

  FDoubleWidthChrLinesView := SynEditStringDoubleWidthChars.Create
                                                            (FTrimmedLinesView);

  {$IFDEF WithSynExperimentalCharWidth}
  FSysCharWidthLinesView := TSynEditStringSystemWidthChars.Create(FDoubleWidthChrLinesView, Self.Canvas);

  FBidiChrLinesView := TSynEditStringBidiChars.Create(FSysCharWidthLinesView);
  FTabbedLinesView := TSynEditStringTabExpander.Create(FBidiChrLinesView);
  {$ELSE}

  {$IFnDEF WithOutSynBiDi}
  FBidiChrLinesView := TSynEditStringBidiChars.Create(FDoubleWidthChrLinesView);
  {$ENDIF}

  // ftab, currently has LengthOfLongestLine, therefore must be after DoubleWidthChar
  {$IFnDEF WithOutSynBiDi }
  FTabbedLinesView := TSynEditStringTabExpander.Create(FBidiChrLinesView);
  {$ELSE}
  FTabbedLinesView := TSynEditStringTabExpander.Create(FDoubleWidthChrLinesView);
  {$ENDIF}

  {$ENDIF} // WithSynExperimentalCharWidth

  // Pointer to the First/Lowest View
  // TODO: this should be Folded...
  FTheLinesView := FTabbedLinesView;
  FTopLinesView := FTrimmedLinesView;

  FFoldedLinesView := TSynEditFoldedView.Create(FTheLinesView, fCaret);
  FFoldedLinesView.OnFoldChanged := @FoldChanged;
  FFoldedLinesView.OnLineInvalidate := @InvalidateGutterLines;
  FFoldedLinesView.DisplayView.NextView := FTheLinesView.DisplayView;

  FDisplayView := FFoldedLinesView.DisplayView;

  // External Accessor
  FStrings := TSynEditLines.Create(TSynEditStringList(FLines), @MarkTextAsSaved); <--
Code: [Select]
constructor TSynEditLines.Create(ATextBuffer: TSynEditStringList; OnSaved: TSavedNotification);
begin
  inherited Create; <--
Code: [Select]
      class function TObject.NewInstance : tobject;

        var
           p : pointer;

        begin
           getmem(p, InstanceSize); <--
           if p <> nil then
              InitInstance(p);
           NewInstance:=TObject(p);
        end;                           

Any idea?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (20H2)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9280
  • FPC developer.
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #1 on: February 09, 2015, 01:03:46 pm »
newisntance is a class method. Do you properly create the class using

instancename:=Tsusedit.create(nil);   ?

Pascal

  • Hero Member
  • *****
  • Posts: 922
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #2 on: February 09, 2015, 01:11:46 pm »
Yes,
looks like this:
Code: [Select]
function TEditorFile.CreateForCopy(ACopyName: String): TsusEdit;
var
  ext: String;
  j, i: Integer;
  EditorFile: TEditorFile;
  newFileName: String;
begin
  Result := nil;
  if not Assigned(fLib) then exit;
  ext := UpCase(ExtractFileExt(ACopyName));
  if ext = '' then ACopyName := ACopyName + '.CBL';
  newFileName := fCobolProject.FindFile(ACopyName);
  if newFileName <> '' then begin
    for j := 0 to fList.Count -1 do begin;
      EditorFile := TEditorFile(fList.Items[j]);
      for i := 0 to EditorFile.CopyList.Count - 1 do begin
        if UpCase(TsusEdit(EditorFile.CopyList.Items[i]).FileName) = UpCase(newFileName) then begin
          //DebugLn('CreateForCopy: Datei gefunden: '+EditorFile.Name+': '+ACopyName);
          Result := TsusEdit(EditorFile.CopyList.Items[i]);
          Result.AddUser(fFileName);
          exit;
        end;
      end;
    end;
    Result := TsusEdit.Create(nil, newFileName, fFileName); [color=red]<--[/color]
    fCopyList.Add(Result);
    Result.Lines.LoadFromFile(newFileName);
  end else begin
    DebugLn('CreateForCopy: '+ACopyName+' nicht gefunden!');
  end;
end; 
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (20H2)

Pascal

  • Hero Member
  • *****
  • Posts: 922
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #3 on: February 09, 2015, 01:12:47 pm »
If i put compiler to -O3 everything works fine!
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (20H2)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9280
  • FPC developer.
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #4 on: February 09, 2015, 01:30:51 pm »
That sounds like you overwrite memory somewhere. Slight variations in compiler params can then make the problem appear and disappear again.

Pascal

  • Hero Member
  • *****
  • Posts: 922
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #5 on: February 09, 2015, 03:06:29 pm »
How can i figure out what went wrong?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (20H2)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9280
  • FPC developer.
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #6 on: February 09, 2015, 03:10:22 pm »
That is hard and requires experience. (read the web about heisenbug)

Anyway, best is to first turn on all checks (-CRriot) and if you are really desperate  you need to use solutions that check all bounds of allocations on every access.

On *nix this can be done (with FPC) with valgrind.

On Windows I don't know. If you are also using Delphi, fastmm4 with fulldebugmode is an option.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7095
  • Debugger - SynEdit - and more
    • wiki
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #7 on: February 09, 2015, 03:59:25 pm »
You should allow asserts -Sa
and define all of the following:

Code: [Select]
SynCheckPaintLock
SynClipboardExceptions
AssertSynMemIndex
SynAssert
SynAssertFold

They switch on a few checks inside SynEdit.

Alternative run in valgrind

Pascal

  • Hero Member
  • *****
  • Posts: 922
Re: [SynEdit] external SIGSEGV at Adress 40AF53: TObject.Newinstance
« Reply #8 on: February 11, 2015, 12:18:39 pm »
Switching on checks brought me to writing to an array out of bounds. After correcting this,
the errror didn't came up again.

Thanks for your help!
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (20H2)

 

TinyPortal © 2005-2018