Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Destroy Problem.

Status
Not open for further replies.

DaZZleD

Programmer
Oct 21, 2003
886
US
I have declared two types of objects. One is called TPers (with data about individuals, like name, age, location...) and TEmployees. TEmployees contains among others, an array of TPers. When exiting the application I get a EInvalidPointer exception. Both classes are descendents of TObject, and contain a destructor:
Code:
destructor Destroy; override;

which is implemented like
Code:
destructor TPers.Destroy;
begin
  inherited destroy;
end;
and
Code:
destructor TEmployees.Destroy;
begin
  inherited destroy;
end;
respectivelly.

If I stop using TEmployees (hence the array of TPers) everything works allright.
From what I've gathered this kind of exception are thrown usually when an attemp to destroy a non-existent object is encountered. So the problem must be that when destroying TEmployees, all of the TPers contained whithin are destroyed as well, and when the destructor of TPers tries to destroy all instances of TPers, some are inexistent so it raises this exception. There certainly is a workaround to this but I'm missing it...

Any help with this is appreciated.

Thanks,
Alex
 
here's something else I found:

the array of TPers is use is a dynamic one. If instead I use a static one, no exceptions are raised...

o_O
 
If you're at D5 or later, consider using a TObjectList instead of an array for the TPers objects. That way, you can simply create the TObjectList when you create a TEmployees and destroy it when you when you destroy the TEmployees. The TObjectList will automatically destroy any objects you add to it (unless you change the default property).
 
Also - keep in mind that if all your destructor is doing is calling it's inherited destructor, then there's no need to declare it at all.
 
I used TList to store my objects and had no problems with it. Thanks for all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top