Thread: delete memory in a struct destructor

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    delete memory in a struct destructor

    I am having trouble deleting the memory i create in a struct. I created a desstructor to do the work but it crashed after it stores it into my link list.

    Code:
    // Globals
    struct menu
    {
    	char* name;
    	unsigned int numOptions;
    	struct option
    	{
    		char* name;
    		bool isMenu;
    		char* fileName;
    	};
    	option* theOptions;
    
    	menu(void) { theOptions = NULL; }
    	~menu(void) { delete [] theOptions; }
    };
    
    // Prototypes
    const menu getMenu(const char* fileName);
    void showMenu(const menu& theMenu, unsigned int currentSelection = 0);
    void gameLoop(void);
    
    int main(void)
    {
    	SLList<menu> menus;
    	menus.addHead(getMenu("main.mnu"));
    
    	gameLoop();
    
    	return 0;
    }

    I am having trouble debugging this one. I don't need an answer on how to fix it, just need a clue as to where to look

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Where do you allocate the memory for theOptions that you're deleting in the destructor? In addHead? Can you show the definition for that? Also, can you explain why you decided to define this as a struct not a class?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Oops... didn't read the part about not wanting an answer...

    Here's a hint. You are using C++, so use C++. If you consider it "lazy" to use standard C++ features, you are more likely to end up with problems like this that are difficult to debug and fix. If you choose to use standard C++ features instead of doing it the un-"lazy" way, your problem will be fixed.

    If you want to fix your problem without using those language tools, look at what happens when getMenu returns a copy of a new menu (or read the answer that I originally posted).
    Last edited by Daved; 03-11-2006 at 11:57 PM.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I don't need an answer on how to fix it, just need a clue as to where to look

    given that you have several raw pointers in your struct...you could start by redefining everything as stl containers (it's usually better to initially use 'known good' containers before optimizing with hand-coded versions).

    Code:
    struct menu
    {
    	struct option
    	{
    		string name;
    		bool isMenu;
    		string fileName;
    	};
    	
    	string name;
    	vector <option> theOptions;
    
    	menu(void) { }
    	~menu(void) { }
    };
    
    // Prototypes
    const menu getMenu(const char* fileName);
    void showMenu(const menu& theMenu, unsigned int currentSelection = 0);
    void gameLoop(void);
    
    int main(void)
    {
    	list<menu> menus;
    	menus.push_back(getMenu("main.mnu"));
    
    	gameLoop();
    
    	return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You MUST read about the "rule of three" it will tell you everything you need to know to fix your problem. Either google for it, or try this link:

    http://codenewbie.com/print_article.php?aid=1504

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    It's amazing how many times I forget to think about the copy/assignment issue. Thanks for pointing out the obvous. ^^

    as far as using a struct, I just felt the object was very simple and didnt really have any use for a class. I Guess it has a lot to do with being lazy also.


    thanks everyone for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM