Sign in Join

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

Mailing lists / fpc-devel / 2010-10

Problem with cascading Destructor in fpc 2.4

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Martin Krüger

Hi

I introduced an boolean-option to the destructor of a class. I implemented a wrapper without argument for reason of backward-compability.

This wrapper calls the real destructor with default argument.

Everything worked fine until fpc 2.4 (sample programm below) When running it compiled with fpc 2.4 i get an Runtime error 216 (segmentation fault).
I could reproduce the problem with the latest svn head.

Without further examination i suggest: The outer destructor tries to free the memory the inner destructor already released.

So my question:
Is it a bug or am i doing something i should not?
The compiler gives me am note:

test.pas(8,3) Note: Class should have one destructor only
Best regards, Martin ------------ program cascading_destructor; type test=class constructor create(); destructor destroy();overload; destructor destroy(arg:boolean);overload; end; constructor test.create(); begin end; destructor test.destroy(); begin writeln('calling master destroy'); destroy(true); writeln('master destroy done'); end; destructor test.destroy(arg:boolean); begin writeln('destroyed'); end; var a:test; begin a:=test.create(); a.destroy; end. type test=class constructor create(); destructor destroy();overload; destructor destroy(arg:boolean);overload; end; constructor test.create(); begin end; destructor test.destroy(); begin writeln('calling master destroy'); destroy(true); writeln('master destroy done'); end; destructor test.destroy(arg:boolean); begin writeln('destroyed'); end; var a:test; begin a:=test.create(); a.destroy; end.

Reply by email

Luca Olivetti
to Martin Krüger

Al 30/10/10 17:54, En/na Martin Krüger ha escrit:

Show quoted text 6 lines
So my question: Is it a bug or am i doing something i should not? The compiler gives me am note: test.pas(8,3) Note: Class should have one destructor only

If I remember correctly you should never call destroy directly, only free (which in turn will call your destructor).

Bye

Luca

Reply by email

Michael Van Canneyt
to Martin Krüger: Hi I introduced an boolean-option to the destructor of a class. I implemented a wrapper…
Martin Krüger wrote 21 lines
Hi I introduced an boolean-option to the destructor of a class. I implemented a wrapper without argument for reason of backward-compability. This wrapper calls the real destructor with default argument. Everything worked fine until fpc 2.4 (sample programm below) When running it compiled with fpc 2.4 i get an Runtime error 216 (segmentation fault). I could reproduce the problem with the latest svn head. Without further examination i suggest: The outer destructor tries to free the memory the inner destructor already released. So my question: Is it a bug or am i doing something i should not? The compiler gives me am note: test.pas(8,3) Note: Class should have one destructor only

This is your problem.

The destructor must always be called destroy (overriding TObject.Destroy) and should accept no arguments. If you want to pass an argument to it somehow, use a property which you set prior to calling destroy.

Michael.

Reply by email

Vinzent Höfler
Martin Krüger wrote
I introduced an boolean-option to the destructor of a class.

Actually, you seem to be violating basic object oriented paradigms here. An object should always know from its current state how to destruct itself, so why should a destructor need a parameter?

Vinzent.

Reply by email