Sign in to follow this  
skusey

Vector holding pointers

Recommended Posts

Hi, I have been stuck with a memory leak the last couple of hours and I really can't work out where I am going wrong. If anyone could advise me on my code it would be much appreciated.
AClass::AClass()
{
	pAStruct* as = new AStruct();
	add(as);
}

// structList is declared as 'vector<AStruct*> structList' within the class
void AClass::add(AStruct* as)
{
	structList.push_back(as);
}

// Called at the end to clean up from outside the class.
void AClass::destroy()
{
	// Delete all stored structs.
	size_t listLen = structList.size();
	for(size_t i=0; i<listLen; i++)	delete structList[e];
}

I have simplified the code, and stripped out the variable names to try and just focus on the actual problem. When the program finishes I am left with a memory leak of 4 bytes, which I think is the size of a pointer? However the actual memory allocated for the struct is released. I'm all confused :S. Is it the way I am passing the pointer to the method? Or the way I am storing the pointers in the vector? Sorry if this is a bit of a silly question, struggling to get my head round it. Any suggestions or help would be great. Cheers!

Share this post


Link to post
Share on other sites
for(size_t i=0; i<listLen; i++) delete structList[e];





What is e? Also, what are you using to identify memory leaks?

More generally, why is it that you need a vector of pointers? If it is necessary, might I suggest you call destroy() from within your destructor, instead of leaving it up to the caller.


jfl.

Share this post


Link to post
Share on other sites
ah sorry my mistake, e is meant to be an i, its not in my actual code. I am using Microsoft Visual C++ 2005, and to be completely honest I'm not sure which switch or option is being used, but it informs me at the end of execution of the debug version if there are any memory leaks. Comes up in the console:


Detected memory leaks!
Dumping objects ->
{317} normal block at 0x00EBD5A8, 4 bytes long.
Data: <( > 28 DA EB 00
Object dump complete.



As for why I use a vector of pointers. Well in the actual program I am writing this class is a particle system, which allows other objects in the game world to add new emitter's (the struct in the code example I gave) to it. The game world objects hold on to the struct and just pass its pointer onto the particle system.

I'm sure I am just doing something stupid after a long days coding :(.

Share this post


Link to post
Share on other sites
An excellent idea, but the option is greyed out on my IDE, god knows why. I read that article, sounds like a very helpful method of debugging if I could only work out how to engage it. I'm going a bit mad with this problem, have used exactly the same method else where with no problems so I really can't begin to work this out. Think I'm going to give up for tonight, start a fresh tomorrow. Cheers for all the suggestions.

Share this post


Link to post
Share on other sites
The option will be greyed out until you actually start a debugging session (don't ask me why). Go to Debug->Step Into (F11) or Step Over (F10), then set the breakpoint, then go to Debug->Continue (F5).


jfl.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this