Re: [Lazarus] Memory corruption investigation

2012-03-22 Thread Felipe Monteiro de Carvalho
On Thu, Mar 22, 2012 at 1:13 AM, Mattias Gaertner
nc-gaert...@netcologne.de wrote:
 Have you tried the keepreleased?

I set it to true as the first item in my program code and also in an
offending unit but couldnt see much of a difference.

I suppose that I should look for which block contains $B73DE820 to see
who allocated the memory, but there are no blocks in the listing:

Marked memory at $B73DE820 invalid
Wrong signature $ instead of 755D2E31
  $08065DEB
  $08065EA7
  $0805C898
  $0812AAEC  TLAZINTFIMAGE__DESTROY,  line 3244 of intfgraphics.pas
  $08057AE2
  $08057AE2
  $0817E53E  TCDWIDGETSET__DELETEOBJECT,  line 863 of
./customdrawn/customdrawnwinapi.inc
  $0816DF2F  DELETEOBJECT,  line 181 of ./include/winapi.inc
  $080FDC2E  TSHAREDRASTERIMAGE__FREEHANDLE,  line 40 of
./include/sharedrasterimage.inc
Heap dump by heaptrc unit
432 memory blocks allocated : 101773/102496
378 memory blocks freed : 92141/92816
54 unfreed memory blocks : 9632
True heap size : 786432
True free heap : 769024
Should be : 773296
Call trace for block $B73DD9C0 size 2160
  $0805C898
  $080DA2C7  TRAWIMAGE__EXTRACTRECT,  line 1576 of graphtype.pp
  $08100EF3  TCUSTOMBITMAP__SETSIZE,  line 342 of ./include/custombitmap.inc
  $08100360  TRASTERIMAGE__SETWIDTH,  line 956 of ./include/rasterimage.inc
  $08080925  TCDDRAWER__SCALERASTERIMAGE,  line 561 of customdrawndrawers.pas
  $08082120  TCDDRAWERANDROID__LOADRESOURCES,  line 482 of
customdrawn_android.pas
  $08080666  TCDDRAWER__CREATE,  line 518 of customdrawndrawers.pas
  $08083265  CUSTOMDRAWN_ANDROID_init,  line 759 of customdrawn_android.pas
// 

The closest one is this first block, but still B73DD9C0 + 2160 is
B73DE230 which is less then B73DE820

-- 
Felipe Monteiro de Carvalho

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Memory corruption investigation

2012-03-22 Thread Felipe Monteiro de Carvalho
Ok, now I found who is causing the problem =)

function TCDWidgetSet.RawImage_CreateBitmaps(const ARawImage:
TRawImage; out ABitmap, AMask: HBitmap; ASkipMask: Boolean): Boolean;
var
  NewData: PByte;
  lRawImage: TRawImage;
  lBitmap: TCDBitmap;
begin
  {$ifdef VerboseCDBitmap}
  DebugLn(Format(':[TCDWidgetSet.RawImage_CreateBitmaps]
ARawImage.Description=%s', [ARawImage.Description.AsString]));
  {$endif}

  Result := False;
  ABitmap := 0;
  AMask := 0;

  // Copy the data
  if ARawImage.DataSize  0 then
  begin
NewData := AllocMem(ARawImage.DataSize); -
System.Move(ARawImage.Data^, NewData^, ARawImage.DataSize);
  end
  else
NewData := nil;
  {$ifdef VerboseCDBitmap}
  DebugLn(Format(':[TCDWidgetSet.RawImage_CreateBitmaps] Data=%x Data
size=%d NewData=%x',
[PtrUInt(ARawImage.Data), ARawImage.DataSize, PtrUInt(NewData)]));
  {$endif}

  // this is only a rough implementation, there is no check against bitsperpixel
  lBitmap := TCDBitmap.Create;
  ABitmap := HBITMAP(lBitmap);
  System.Move(ARawImage, lRawImage, SizeOf(TRawImage));
  lRawImage.Data := NewData; -
  lBitmap.Image := TLazIntfImage.Create(lRawImage, True); -
  Result := ABitmap  0;

This shows like this in the log:

:[TCDWidgetSet.RawImage_CreateBitmaps] Data=B73FCC90 Data size=3600
NewData=B73FE360

And later on:

Marked memory at $B73FE360 invalid --- Same address!
Wrong signature $ instead of AFB54FD8
  $08065DDB
  $08065E97
  $0805C888
  $0812AADC  TLAZINTFIMAGE__DESTROY,  line 3244 of intfgraphics.pas
  $08057AD2
  $08057AD2
  $0817F638  TCDWIDGETSET__DELETEOBJECT,  line 863 of
./customdrawn/customdrawnwinapi.inc
  $0816DF1F  DELETEOBJECT,  line 181 of ./include/winapi.inc
  $080FDC1E  TSHAREDRASTERIMAGE__FREEHANDLE,  line 40 of
./include/sharedrasterimage.inc

I use AllocMem and TLazIntfImage uses:

procedure TLazIntfImage.FreeData;
begin
  if FDataOwner
  then ReallocMem(FRawImage.Data, 0)


-- 
Felipe Monteiro de Carvalho

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Memory corruption investigation

2012-03-22 Thread Felipe Monteiro de Carvalho
Ok, I found it! =D

Fixed in rev 36215.
-- 
Felipe Monteiro de Carvalho

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


[Lazarus] Memory corruption investigation

2012-03-21 Thread Felipe Monteiro de Carvalho
Hello,

I thought that maybe someone might have an idea about how to
investigate an issue of memory corruption / freeing unallocated area:

Basically I would like to understand how I can read the info from
heap-trace. I have this:

:[TCDWidgetSet.CreatePenIndirect]  Style: 0, Color: D300 Result:B74D3760
:[TCDWidgetSet.SelectObject] DC=B74BBBC0 GDIObj=B74D3760
:[TCDWidgetSet.SelectObject] Result=B74D3860 ObjectType=Pen

Trace: [WinAPI DeleteObject] GDIObject: B74E37E0
Trace: [WinAPI DeleteObject] Result=True ObjectType=Image
:[TCDWidgetSet.SelectObject] DC=B74BBAF0 GDIObj=B74D35E0
:[TCDWidgetSet.SelectObject] Result=B74D34E0 ObjectType=Pen
Trace: [WinAPI DeleteObject] GDIObject: B74E3600
Marked memory at $B7509820 invalid
Wrong signature $ instead of 6C48700E
  $0806452B
  $080645E7
  $0805BCE8
  $08126A4C  TLAZINTFIMAGE__DESTROY,  line 3244 of intfgraphics.pas
  $080570D2
  $080570D2
  $0817AAB8  TCDWIDGETSET__DELETEOBJECT,  line 867 of
./customdrawn/customdrawnwinapi.inc
  $081699DF  DELETEOBJECT,  line 181 of ./include/winapi.inc
  $080FB14E  TSHAREDRASTERIMAGE__FREEHANDLE,  line 40 of
./include/sharedrasterimage.inc
Trace: [WinAPI DeleteObject] GDIObject: B74D3760
Trace: [WinAPI DeleteObject] Result=True ObjectType=Pen
Trace: [WinAPI DeleteObject] GDIObject: B74D34E0
Trace: [WinAPI DeleteObject] Result=True ObjectType=Pen
Heap dump by heaptrc unit
1414 memory blocks allocated : 132878/141032
1359 memory blocks freed : 123243/131352
55 unfreed memory blocks : 9635
True heap size : 786432
True free heap : 768928
Should be : 773232
Call trace for block $B74F7380 size 17
  $0817A934  TCDWIDGETSET__DELETEOBJECT,  line 795 of
./customdrawn/customdrawnwinapi.inc
  $081699DF  DELETEOBJECT,  line 181 of ./include/winapi.inc
  $080FB14E  TSHAREDRASTERIMAGE__FREEHANDLE,  line 40 of
./include/sharedrasterimage.inc
  $080FB2F9  TSHAREDCUSTOMBITMAP__FREEHANDLE,  line 36 of
./include/sharedcustombitmap.inc
  $080FE400  TCUSTOMBITMAP__SETSIZE,  line 367 of ./include/custombitmap.inc
  $080FD800  TRASTERIMAGE__SETWIDTH,  line 956 of ./include/rasterimage.inc
  $0807F14B  TCDDRAWER__SCALERASTERIMAGE,  line 558 of customdrawndrawers.pas
  $08080720  TCDDRAWERANDROID__LOADRESOURCES,  line 433 of
customdrawn_android.pas


Does Heaptrace end the application by itself when it finds the wrong
signature? If yes, why there is after that more DeleteObject calls?

From what I understand I see that it created a Pen with address
B74D3760 and later tried to free this same object which brought the
error.

So, what happened here? Another code wrong in that address space
allocated for the Pen? Or would the error be in the Pen code itself? I
already read it and can't find any error.

thanks,
-- 
Felipe Monteiro de Carvalho

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Memory corruption investigation

2012-03-21 Thread Felipe Monteiro de Carvalho
Aha, I think it has nothing to do with the Pen ... I hate when various
DebugLn calls get out of sync and override one another.

Still open to suggestions of how to debug this =)

The error is a crash freeing TLazIntfImage when it frees the data.

-- 
Felipe Monteiro de Carvalho

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Memory corruption investigation

2012-03-21 Thread Mattias Gaertner
On Wed, 21 Mar 2012 11:33:22 +0100
Felipe Monteiro de Carvalho felipemonteiro.carva...@gmail.com wrote:

 Hello,
 
 I thought that maybe someone might have an idea about how to
 investigate an issue of memory corruption / freeing unallocated area:

Have you tried the keepreleased?

Mattias

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus