[Solved]STD Vector and Dynamic Memory Clean up

Started by
13 comments, last by jpetrie 17 years, 5 months ago
Why not just use boost shared_ptr? Smart pointers are a wonderful thing.
Programming since 1995.
Advertisement
Quote:Original post by phil_t
Quote:Original post by ChaosPhoenix
the program. I don't *think* that node is the problem, I find it odd that all elements of the vector can be deleted fine, except for the final node.


Maybe you already deleted root somewhere in your program?



I thought that might be it as well, but delete isn't called anywhere except for that function I posted above.

All the tNode's are dynamically created and then pushed into the vector, so it should be my responsibility to clean them up.

As for everyone suggesting Boost; I'm not a huge fan of the boost library, I understand it can make things like this easier, but I'd rather do it the old fashion way and find out WHY this is crashing instead of going around it.
"As for everyone suggesting Boost; I'm not a huge fan of the boost library, I understand it can make things like this easier, but I'd rather do it the old fashion way and find out WHY this is crashing instead of going around it."

That's the right attitude. You might want to consider moving to smart pointers once you have solved the problem, despite the slightly less-natural syntax.
I figured out the problem. It was related to that root pointer.

I use a very basic sorted list for this AI application. The sort function looks like this:

	// If our list is empty	if(m_Open.empty())		m_Open.push_front(pNewNode); // Add them to the front	// Otherwise, for each item in the list...	for(list<tNode*>::iterator iter = m_Open.begin(); iter != m_Open.end(); iter++)	{		// Is this node better than one in the list? 		if(pNewNode->tCost < (*iter)->tCost)		{			m_Open.insert(iter, pNewNode); // Yes, insert it			return;		}    }	// Node goes to the back	m_Open.push_back(pNewNode);


As you can see I make a bit of a special case for the 1st node placed into the list. Going on this hunch I decided to just remove this section of code:

// If our list is empty	if(m_Open.empty())		m_Open.push_front(pNewNode); // Add them to the front


And behold, it doesn't crash any longer. As to why this was corrupting, or deleteing, my root node - I don't know, but the issue is solved. The debugger does toss out a few first-chance exceptions in the output window, but it handles it gracefully(i.e. doesn't crash) so I'm not too worried.

I do appreciate all the advice so far, and will look into smart pointers and such in the future.

EDIT: Fixed the first-chance exceptions, no errors at all any longer.
While I disagree with most of the AP's other comments (on the grounds that they're mostly stylistic, and there's no accounting for taste), I'm generally of the same opinion on this particular issue.

Smart pointers and all that are wonderful, but using them in place of regular pointers without at least some knowledge of pointers is usually just asking for trouble (not that I'm accusing the OP of not knowing anything about pointers, I'm just making a general statement) down the line.

Furthermore, a solution that is not predicated on the cause of the problem is not a solution at all, it's just a band-aid (and here the OP, AP and I agree). Especially since "_BLOCK_TYPE_IS_VALID" errors often imply a memory over- or under-write, which a smart pointer doesn't protect you against. A double-delete (which a smart pointer can protect against) is also a possibility. The OP's problem is likely one of the two, and if the former it won't be in the delete code but rather some other portion of the algorithm.

Fix the problem first, and then consider preventative measures like smart pointers in the future.

This topic is closed to new replies.

Advertisement