Crash when freeing memory.

This is a discussion on Crash when freeing memory. within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem when I try to free memory. I have my own struct to store info, its called ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Crash when freeing memory.

    I'm having a problem when I try to free memory. I have my own struct to store info, its called SoundNode, and I have an STL vector to store all the SoundNodes in the scene, here is the code:
    Code:
    std::vector<SoundNode *> SceneNodes;
    Now in the destructor of my scene manager I call a function called freeMemory() that frees all the memory allocated before (I don't put the code in the actual destructor because in some cases I want to free the memory of the scene manager without actually destroying the object). Now my freeMemory() function looks like this:
    Code:
    void SceneManager::freeMemory()
    {
    	for(int i = 0; i < (int)SceneNodes.size(); ++i)
    	{
    		if(SceneNodes[i])
    			delete SceneNodes[i];
    		SceneNodes[i] = NULL;
    	}
    	SceneNodes.clear();
    }
    Now, when I run this in debug mode inside Visual Studio 2003 everything works fine. But if I run the same executable outside Visual Studio the application crashes when I try to delete SceneNodes[i]. Any idea what's wrong with my code?

    Thanks in advance.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Are you sure that all of the SceneNodes are allocated? You might want to make sure that all SceneNode pointers inserted into the vector are initialized to NULL. Further, if you delete these pointers anywhere else, set them to NULL.

    By the way, the [i]if(SceneNodes) line is unnecessary. The delete operation is safe on a NULL pointer. You can change it to the following:
    Code:
    void SceneManager::freeMemory()
    {
    	for(int i = 0; i < (int)SceneNodes.size(); ++i)
    	{
    		delete SceneNodes[i];
    		SceneNodes[i] = NULL;
    	}
    	SceneNodes.clear();
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Why not also use a vector for storing all your SoundNodes ?
    Code:
    std::vector< std::vector<SoundNode> > SceneNodes;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Make sure you aren't adding any pointers to the vector twice, or to different vectors. Make sure that the vector is the only place that the pointers are deleted. Make sure you aren't adding non-null non-dynamically allocated pointers to the vector.
    Quote Originally Posted by Salem
    Why not also use a vector for storing all your SoundNodes ?
    Code:
    std::vector< std::vector<SoundNode> > SceneNodes;
    The vector contains SoundNode pointers, not SoundNode arrays, so it doesn't make sense to have a vector of vectors. I am basing this assumption on the delete SceneNodes[i] code instead of delete [] SceneNodes[i].

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Quote Originally Posted by Daved
    The vector contains SoundNode pointers, not SoundNode arrays, so it doesn't make sense to have a vector of vectors. I am basing this assumption on the delete SceneNodes[i] code instead of delete [] SceneNodes[i].
    I beleve the suggestion was not so much that and array of arrays (or a vector of vetors) is such a good structure, but that it is a way to ignore the issue of dynamic allocation entirely. Someone who is performing a double delete (a very popular way to crash on delete) probably hasn't picked up on the subtle difference between delete and delete [] pointer to object, pointer to array of objects, etc... That being said, if you must mix dynamic allocation with stl containers my solution is boost::shared_ptr, if you really need Java like construction rules. However, chances are you don't need dynaimic user-managed memory at all. For example if soundrec's can be in any order you can "delete" arbitrary records by swapping a record with the end, then pop_back() on the vector. Deque is also popular in such settings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Freeing memory before exit
    By tretton in forum C Programming
    Replies: 13
    Last Post: 02-01-2006, 06:33 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21