Sign in to follow this  

Memory leak in release build but not in debug?

This topic is 4398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

according to my memleak detection (overloading new/delete and logs into a file, only the memory that hasn't been deleted) The problem is that my BST priority queue apperantly leaking memory not deleting the nodes in the tree when dequeueing an item, But only when it is compiled in release mode it doesn't report any memoryleak in debug. How can I find the leak in release mode? This is the source code of my BST priority queue have not implemented any balancing functions yet.
#ifndef CBSTPriorityQueue_H
#define CBSTPriorityQueue_H

#include <iostream>
#include <iomanip>
using namespace std;

	/**
		Priority queue exceptions
		This is the different types of exceptions the priority queue can or will cast.
	*/
	enum CPriorityQueueExceptions {
							QUEUE_EMPTY,				//!< Illegal operation on an empty queue.
							QUEUE_INDEX_OUT_OF_RANGE,	//!< Index out of range.
							QUEUE_UNKNOWN				//!< Undefined error.
							};

	template <class T>
	class CBSTPriorityQueue {

		class CNode {
		public:

			T		m_Item;		//!< The item to store

			CNode	*m_pLeft;	//!< Root node of left sub tree.
			CNode	*m_pRight;	//!< Root node of right sub tree.

			/**
				Simple constructor

				@param i - Item to store
				@param l - Left node
				@param r - Right node
			*/
			CNode( T i, CNode *l, CNode *r )
			{ m_Item = i; m_pLeft = l; m_pRight = r; }

			/**
				Insert new item into tree.

				@param o - The item to insert.
			*/
			void insert( T o )
			{
				if( o < m_Item )
				{
					// object has lower priority, insert to the left.
					if( m_pLeft != NULL )
						m_pLeft->insert(o);
					else
						m_pLeft = new CNode(o, NULL, NULL);
				}
				else if ( o >= m_Item )
				{
					// object has higher or equal priority, insert to the right.
					if( m_pRight != NULL )
						m_pRight->insert(o);
					else
						m_pRight = new CNode(o, NULL, NULL);
				}
			}
		};

		int		m_iSize;	//!< Size of tree.
		CNode	*m_pRoot;	//!< Root node of tree.

		/**
			finds the node that has the item with highest priority.
			and delete the node.

			@return The item with highest priority.
		*/
		T removeMax()
		{
			assert( !isEmpty() );

			CNode *removeMe = m_pRoot;
			CNode *parent = NULL;

			while( removeMe->m_pRight != NULL )
			{
				parent = removeMe;
				removeMe = removeMe->m_pRight;
			};

			// Make the parent->right point to the removeMe left node.
			if( parent != NULL )
				parent->m_pRight = removeMe->m_pLeft;
			else if( m_pRoot != removeMe )
				m_pRoot = removeMe->m_pLeft;

			// save item
			T item = removeMe->m_Item;

			// delete the node
			delete removeMe;

			// return the item
			return item;
		}

		/**
			finds the node that has the item with highest priority.

			@return The item with highest priority.
		*/
		T getMax()
		{
			if( isEmpty() )
				throw QUEUE_EMPTY;

			CNode *node = m_pRoot;

			while( node->m_pRight != NULL )
			{
				node = node->m_pRight;
			};

			// return the item
			return node->m_Item;
		}

	public:

		/**
			Initializes the tree.
		*/
		CBSTPriorityQueue()
		{
			m_iSize = 0;
			m_pRoot = NULL;
		}

		/**
			Destroys the tree.

			@see destroyQueue().
		*/
		~CBSTPriorityQueue()
		{
			destroyQueue();
		}

		void enqueue( T o )
		{
			if( isEmpty() )
			{
				// Queue is empty insert first
				m_pRoot = new CNode( o, NULL, NULL );
			}
			else
			{
				m_pRoot->insert(o);
			}
		}

		T dequeue()
		{
			if( isEmpty() )
				return NULL;

			if( m_pRoot->m_pLeft == NULL && m_pRoot->m_pRight == NULL )
			{
				T item = m_pRoot->m_Item;

				delete m_pRoot;

				m_pRoot = NULL;

				return item;
			}
			else
				return removeMax();
		}

		void destroyQueue()
		{
			while( !isEmpty() )
			{
				dequeue();
			};
		}

		T peek() { return getMax(); }

		bool isFull() { return false; }

		bool isEmpty() { return (m_pRoot == NULL); }

		int getSize() { return m_iSize; }
	};

#endif // CBSTPriorityQueue_H



it reports a leak in the insert function where I create the root node and where I create a child node if the root node already exists. EDIT: Just find out that if I add a dequeue line after the enqueue line like this BST->enqueue( MyItem ); BST->dequeue(); works, without any memory leak. so does it behave like this because the dequeue is in a .dll file and the enqueue is in the .exe file? the BST is created in the .dll file and the .exe retrives a pointer to it.

Share this post


Link to post
Share on other sites
Switch both the dll and exe to use the Multi-Threaded DLL for the CRT.
You have to set it in both Debug and Release build for both projects (there's a debug version for debug builds).

If they use the single-threaded or statically linked CRT that have will have seperate heaps and free-stores.

Share this post


Link to post
Share on other sites
it is already set to use Multithreaded DLL as runtime library, it was so by default in VC++ 2005 Express. it doesn't work anyway.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When running in debug mode variables are initialized with default values, you probably read some uninitialized memory.

Share this post


Link to post
Share on other sites
I have noticed that my application leaks memory in debug mode too but that my memoryleak library doesn't catch that leak in debug mode but the TaskManager in WinXP does.

I have tried my BST in a simple loop like this:

#include "CBSTPriorityQueue.h"

class CTest {
public:
int m_iValue;
};

CBSTPriorityQueue<CTest*> PQ;

int main(void)
{

CTest *t = new CTest;
while( 1 ) // neverending loop just to see if memory increase in taskmanager
{
PQ->enqueue( t );
t = PQ->dequeue();
};
delete t;

return 0;
}



That code doesn't result in any memory leak. even though I do the same in my graphics eninge it does eat my memory. in debug my memorytracer doesn't say that I miss any delete, but in release build the memory tracer says I don't delete the nodes I insert in the BST

Share this post


Link to post
Share on other sites
Sign in to follow this