Sign in Join

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

Mailing lists / fpc-pascal / 2011-08

New Dispose and FreeMem and objects

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Andrew Haines

Hi is the following allowed or will it cause problems:

type
PObj = ^TObj;
TObj = object
  constructor Init;
end;
var
 Obj : PObj;
begin
 Obj := new(PObj, Init);
 FreeMem(Obj); // This is the line I wonder about.
end;

I compiled this and it doesn't throw any exceptions.

Should I only use Dispose(Obj) ?

Maybe Dispose can call a destructor and that is the difference between Dispose and FreeMem?

Thanks,

Andrew Haines

Reply by email

Jonas Maebe
to Andrew Haines
Andrew Haines wrote
Maybe Dispose can call a destructor and that is the difference between Dispose and FreeMem?

The other difference is that dispose() will finalize any reference counted fields, while freemem won't.

Jonas

Reply by email

Andrew Haines
to Jonas Maebe
Jonas Maebe wrote 9 lines
On 26 Aug 2011, at 22:39, Andrew Haines wrote: > Maybe Dispose can call a destructor and that is the difference between > Dispose and FreeMem? The other difference is that dispose() will finalize any reference counted fields, while freemem won't.

Ok so freemem is okay as long as I am careful about the members of the object.

Thanks,

Andrew Haines

Reply by email

Jonas Maebe
to Andrew Haines
Andrew Haines wrote
Ok so freemem is okay as long as I am careful about the members of the object.

There may be other problems I'm not thinking of right now. I don't understand why you want to use freemem for objects, since the only difference with dispose is that it will result in crashes or memory corruption under certain circumstances.

Jonas

Reply by email