Advertisement

Object Deconstructors

Started by May 08, 2000 04:08 PM
3 comments, last by Zipster 24 years, 7 months ago
OK heres the problem. I have a program that uses the new and delete operators to add objects to a CObList. However, ever time i run the program, I lose about 2% of system resources. Memory Leaks. But then, when I was thinking about, doesn't the CObList deconstructor release all the pointers in the list and their associated memory? I don't remember reading any docs that say to manual call delete on each pointer in the list. Any thoughts? OH, and i only use new and delete in my program when im adding things to the CObList, just to refine that that's where the memory leaks are occuring. Edited by - Zipster on 5/8/00 4:09:31 PM
If you add something with "new", it''s your responsibility to get rid of it with "delete".

aig
aig
Advertisement
Say I have a class CFoo and a variable:
CFoo myFoo;

When I do this:
delete myFoo;

the application is going to execute:
CFoo::~CFoo
I think I see what you are getting at. Consider a CObList with an array of Objects. This can be declared four ways:

1) COb myList[MAX_OBS]; (stack allocated array of objects)

2) COb* myList[MAX_OBS]; (stack allocated array of object pointers)

3) COb* myList; (heap allocated array of objects)

4) COb** myList; (heap allocated array of object pointers)

In the first two cases the default CObList destructor will free the memory used by the array elements. The difference is that in the first example, those elements are the actual objects (so they will be freed) but in the second case, those array elements are just pointers, so the pointers will be freed but the objects themselves will remain.

In the 3rd and 4th examples, the case is similar to the first two. The only difference is that you will have to call new[] to create the list initially, so you will have to explicitly call delete[] to free it (make sure you add teh [] to the delete command since you are deleting an array!)

Hope that clears things up!

Check out the GPI project today!
Oh, i see, i misunderstood the MSDN documentation:
quote: When a CObArray object is deleted, or when its elements are removed, only the CObject pointers are removed, not the objects they reference.


i thought this meant that they also delete the memeory. Ok, i got it now, thx.

PCMCIA - People Can't Memorize Computer Industry Acronyms
ISDN - It Still Does Nothing
APPLE - Arrogance Produces Profit-Losing Entity
SCSI - System Can't See It
DOS - Defunct Operating System
BASIC - Bill's Attempt to Seize Industry Control
IBM - I Blame Microsoft
DEC - Do Expect Cuts
CD-ROM - Consumer Device, Rendered Obsolete in Months
OS/2 - Obsolete Soon, Too.
WWW - World Wide Wait
MACINTOSH - Most Applications Crash; If Not, The Operating System Hangs

This topic is closed to new replies.

Advertisement