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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running out of virtual memory - don't think it's a memory leak

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo.
I'm working on a program that creates & destroys alot of (fairly simple) objects that, by around the 200th iteration, eats all of the system's virtual memory.
I get a M$ message telling me the system is low on virtual memory, and then my program aborts with an unnatural termination.
I've gone over the code w/ a fine tooth comb & cannot see any place where I would have a memory leak - all objects created on the heap are fully accounted for, as far as I can see.
Of course, I could still be missing something, but I wondered if anybody had any ideas as to anything else this could be, as even if I go back to the drawing board, I need to be writing this kind of program (genetic algorithms) for a while, & need to know how to deal with it.
Thanks in advance for any light anyone can shed.
Cheers,
DJL

Common sense is what tells you the world is flat.
 
Do you delete the allocated memory the correct way???

char * Something = new char[xx];
should be deleted like this:
delete [] Something; // Good for ANY arrays
NOT like these:
delete Something; // Good for char, int, long and such

Totte
 
It's a little lazy, but I've put all objects into vectors that I explicitly loop through in all destructors. Eg:

for(u_int indx(0);indx<the_vector.size();indx++)
delete the_vector[indx];

Like I say, this is a fairly simple program, and every pointer seems to be accounted for.
Chances are I missed something, I guess, but I'm damn'ed if I can spot it.
I was just wondering if anyone knew of any other little wierdities that maybe could cause this.
Or had any handy tips on debugging scenarios like this that I'm unaware of - I'm unaware of alot of things, so please don't hold back!
Cheers,
DJL

Common sense is what tells you the world is flat.
 
Shouldn't that be:
for(u_int indx(0);indx<the_vector.size();indx++)
delete [] the_vector[indx];
????
If you're allocating a lot of vectors which points on arrays you should use the 'delete []' method and not the 'delete' (without the '[]')

Lazy or not, if you're 'delete' an array in stedd of 'delete[]' you WILL have memory leak!

Totte
 
To find out what you have missed could you not have enabled code guard and placed a application->terminate in your program, keep moving it towards the end until it finds the leak ?
 
Totte - I'm not entirely sure what you mean.
The the vectors always contain pointers to objects created on the heap (eg. vector<Tmy_class*>), so shouldn't my code above be sufficient for these? When these objects are deleted, they in turn call similar code on any vectors of pointers they might posses in their destructors, so that should be taken care of.
Or have I totally misunderstood this issue/what you were saying?

Sherak - I'm afraid I know absolutely nothing about code guard - is this something you'd reccommend I'd look into?
& do you know of any good places to look for info on this?

& thanks both for your replies!
Cheers,
DJL

Common sense is what tells you the world is flat.
 
From your sample you're only deleting objects elements of vector(s) point to, but not vector elements! I don't know how many vectors you have, but this way your vectors are getting bigger and biggers since you're not emptying them...
 
Well, if it's classes your'e deleting the 'delete' (without '[]') should do but are you calling the destructors of those classes before shutting them down???

I must admit that i'm a bit into deep water here but if you activates one or more classes and those classes reserves some memory, then you MUST call the destructors of those classes BEFORE terminating their pointers.

AFAIK the 'delete' does NOT call the destructor of a class, by creating and deleting a lot of dynamic classes you MUST allow those classes to clean up after themselves.


Totte
 
Sussed it!
It WAS me being stupid, after all.
Quelle suprise!
The way I was deleting vectors was not an issue - that works fine.
The problem was in a sort function I had written that created a temporary vector & deleted it w/out first iterating through & deleting the (now-defunct) objects within.
Man!
Thanks everyone for their input - sorry it was for such a doofus!
Cheers,
DJL

Common sense is what tells you the world is flat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top