Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-03

Disposing pointers and memory leaks

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Joseph Montanez

Im struggling to get into memory management and im having issues.

      New(Cursor);
      WriteLn('New Cursor');
      Cursor := DrawPixel(Screen, X, Y, 253, 253, 253);
      WriteLn('Assign Cursor');
      SDL_Flip(Screen);
      WriteLn('Render Cursor');
      {It dies here were did cursor go?}
      Dispose(Cursor);
      WriteLn('Dispose Cursor');

An unhandled exception occurred at $00407C9A :
EAccessViolation : Access violation
$00407C9A
$0040182C

It never makes it to Dispose(Cursor);, so my best guess is something else removed it before I did? Im using SDL library so maybe it cleans up for me. But if not then maybe I can getting the wrong idea here. And if it was removed before I try to remove it is there a way to check if the pointer is still in memory?

Paul Ishenin
to Joseph Montanez
Joseph Montanez wrote
New(Cursor);

You allocated memory for Cursor

Cursor := DrawPixel(Screen, X, Y, 253, 253, 253);

You assigned something to Cursor. Thus it is not pointed anymore to your previously allocated memory.

Dispose(Cursor);

You are trying to deallocate memory to which Cursor is pointing. Don't forget that it is pointing to the memory allocated by New anymore after that assignment.

I suppose you need to remove both New and Dispose.

Best regards,
Paul Ishenin.

Joseph Montanez
to Paul Ishenin

Paul,

No wonder i had memory that kept growning. Thank you soo much!

Paul Ishenin wrote 28 lines
Joseph Montanez wrote: > >      New(Cursor); > You allocated memory for Cursor > >      Cursor := DrawPixel(Screen, X, Y, 253, 253, 253); > You assigned something to Cursor. Thus it is not pointed anymore to your previously allocated memory. > >      Dispose(Cursor); > You are trying to deallocate memory to which Cursor is pointing. Don't forget that it is pointing to the memory allocated by New anymore after that assignment. I suppose you need to remove both New and Dispose. Best regards, Paul Ishenin. _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal