[Lazarus] Memory corruption investigation

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Thu Mar 22 11:45:31 CET 2012


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 $AAAAAAAA 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




More information about the Lazarus mailing list