Recent

Author Topic: Freeing Memory causes SIGSEGV Error  (Read 3821 times)

itwhiz

  • New member
  • *
  • Posts: 5
Freeing Memory causes SIGSEGV Error
« on: June 05, 2011, 12:55:53 am »
Getting back into writing Pascal Code, and I'm trying the following code, all it does is loads a JPG, rotates it and then displays it.

Procedure DisplayPiece(X,Y,PieceNum,Rotation: Integer);
Var

    jpg : TJPEGImage;
    bmp : TBitMap;
    bmp2 : TBitmap;
    rect : TRect;
    TileFileName  : string;
Begin
 Try
  TileFileName := GetCurrentDir + '/Tiles/' + inttostr(PieceNum) + '.JPG';
  jpg :=TJPEGImage.Create;
  jpg.LoadFromFile(TileFileName);
  bmp :=TBitmap.Create;
  bmp.assign(jpg);
  bmp2 := Tbitmap.Create;

  bmp2 := (Rotate(bmp,rotation));



  rect := Main.Drawgrid1.CellRect((X),(Y));
  Main.Drawgrid1.Canvas.StretchDraw(rect,bmp2);
  Board_Array [X,Y] := Piece_array[PieceNum];
 Finally
 End;
  bmp.Free;
  bmp2.Free;

end;             


I've included the 2 .Free commands to manage memory usage, but when it hits the 2nd one my program errors.

I'm using Ubuntu & Windows XP and Lazarus 0.9.26.2(on Ubuntu) & 0.9.28.2(On XP), FreePascal 2.2.4 on both.

Any ideas ?


Blaazen

  • Hero Member
  • *****
  • Posts: 3006
  • POKE 54296,15
    • Eye-Candy Controls
Re: Freeing Memory causes SIGSEGV Error
« Reply #1 on: June 05, 2011, 01:32:42 am »
Hello,

1) You have VERY old versions of both freepascal and lazarus. Last stable are 0.9.30 & 2.4.4
2) You did not specify the error message.
3) TileFileName := GetCurrentDir + '/Tiles/' + inttostr(PieceNum) + '.JPG';
  - is not a cross-platform path (= will not work on Windows)
4) I cannot test all your code, Rotate is probably your own function, but
Code: [Select]
Try
  TileFileName := '/media/disk/v1/Obrázky/Zoot/toxiqueb.jpg';  //my own bitmap
  jpg :=TJPEGImage.Create;
  jpg.LoadFromFile(TileFileName);
  bmp :=TBitmap.Create;
  bmp.assign(jpg);
  bmp2 := Tbitmap.Create;
  bmp2 := bmp;
  Image2.Picture.Assign(bmp2);  //I added this line for test purposes only
  { (Rotate(bmp,rotation));
  rect := Main.Drawgrid1.CellRect((X),(Y));
  Main.Drawgrid1.Canvas.StretchDraw(rect,bmp2);
  Board_Array [X,Y] := Piece_array[PieceNum];    }
 Finally
 End;
  bmp.Free;
  bmp2.Free;
  jpg.Free;
Causes crash on line " bmp2.Free;" because - in fact - it tries to free "bmp" twice.
It works when I use:
Code: [Select]
bmp2.Assign(bmp);
instead of
Code: [Select]
bmp2 := bmp;And you should do
Code: [Select]
jpg.Free;to avoid memory leak.
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

itwhiz

  • New member
  • *
  • Posts: 5
Re: Freeing Memory causes SIGSEGV Error
« Reply #2 on: June 05, 2011, 01:53:09 am »
Thanks very much for the reply, as I said I've been away from this too long - SQL Server/Oracle developer for 10 years.

I'll upgrade my systems tomorrow to the latest version and re-write my code.

I do agree with you that the TileFileName command should not work on windows.
If I switch the '/' to '\' then it works on Windows but not on Ubuntu, but this way round '/' it seems to work on both.

Thanks again for the help. I'm sure I'll have some more questions very soon.


Blaazen

  • Hero Member
  • *****
  • Posts: 3006
  • POKE 54296,15
    • Eye-Candy Controls
Re: Freeing Memory causes SIGSEGV Error
« Reply #3 on: June 05, 2011, 02:07:25 am »
You can use constant "DirectorySeparator", it is then done automaticly ('/' for Linux and '\' for Win)
Code: [Select]
TileFileName := GetCurrentDir +DirectorySeparator+'Tiles'+DirectorySeparator+ inttostr(PieceNum) + '.JPG';
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018