Sign in Join

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

Mailing lists / fpc-pascal / 2011-02

Memory leak or object destruction?

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Bo Berglund

I am curious as to how FPC handles the following situations:

var
  MyObj: TsomeObject;
begin
  MyObj := TsomeObject.Create;
  // do some processing
  MyObj := NIL;
end;

Will this create a memory leak or will FPC fnd by reference counting that nothing references the object memory and dispose of it?

If the latter, will the disposal call the object's destructor or not?

next:

var
  MyObj: TsomeObject;
begin
  MyObj := TsomeObject.Create;
  // do some processing
end;

What happens here where the local variable MyObj gets out of scope, again with the reference to object memory disappearing? Memory leak or object destruction?

Bo Berglund Developer in Sweden

Reply by email

Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 12 lines
I am curious as to how FPC handles the following situations: var MyObj: TsomeObject; begin MyObj := TsomeObject.Create; // do some processing MyObj := NIL; end; Will this create a memory leak or will FPC fnd by reference counting that nothing references the object memory and dispose of it?

It will create a memory leak (as in Delphi).

Show quoted text 15 lines
If the latter, will the disposal call the object's destructor or not? next: var MyObj: TsomeObject; begin MyObj := TsomeObject.Create; // do some processing end; What happens here where the local variable MyObj gets out of scope, again with the reference to object memory disappearing? Memory leak or object destruction?

Memory leak. Class instances that are created must also be destroyed.

100% the same as in Delphi.

Michael.

Reply by email

Andrew Hall
to Bo Berglund: I am curious as to how FPC handles the following situations: var MyObj: TsomeObject; begin

Interfaces support reference counting.

ISomeObject = interface

TSomeObject = class(TinterfacedObject, ISomeObject)

var
 MyObj: ISomeObject;

Regards,

Bo Berglund wrote 6 lines
Will this create a memory leak or will FPC fnd by reference counting that nothing references the object memory and dispose of it? What happens here where the local variable MyObj gets out of scope, again with the reference to object memory disappearing? Memory leak or object destruction?

Reply by email