Freeing Memory causes SIGSEGV Error lazarus / 39-Graphics ---------------------------------------------------------------------- itwhiz · 2011-06-04 22:55 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 · 2011-06-04 23:32 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. ---------------------------------------------------------------------- itwhiz · 2011-06-04 23:53 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 · 2011-06-05 00:07 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';