CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Feb 2009
    Posts
    9

    Question Simple class, delete exception

    I apologize if this is a very simple error that has slipped my mind, but does anyone see anything wrong with this class?

    Code:
    class Cell {
    public:
    	Cell() {
    		o1 = new GameObject();
    		o2 = new GameObject();
    		o3 = new GameObject();
    		o4 = new GameObject();
    	}
    	~Cell() {
    		delete o1;
    		delete o2;
    		delete o3;
    		delete o4;
    	}
    private:
    	GameObject * o1;
    	GameObject * o2;
    	GameObject * o3;
    	GameObject * o4;
    };
    When the program is exited and the destructor runs, I get this error:

    Unhandled exception at 0x68b631ea (msvcr90d.dll) in Erg.exe: 0xC0000005: Access violation reading location 0xfeeefee2.

    I realize this means I am trying to delete something that is not there. Should I be checking that the allocation ran properly? Is the constructor somehow not running?

    I'm running Visual Studio 2008 on Windows Vista Home Premium.

    Thanks for any help and sorry again if it's something small and stupid.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Simple class, delete exception

    You code looks ok, and the default constructor and destructor are always called. Did you use a debugger to confirm that the value of 'o1' to 'o4' actually is 0xfeeefee2 ? If all the 4 variables contain valid pointers, maybe there is a problem with the GameObject you are trying to delete.

  3. #3
    Join Date
    Feb 2009
    Posts
    42

    Re: Simple class, delete exception

    You don't have any problem.

    However you should write

    if(o1)
    {
    delete o1;
    o1 = NULL;
    }

    Possibly you are not deleting Cell class properly,

    If you have ie defined some variable globally, as
    Cell g_MYCELLS[10][10];

    destructor is called on the end of program exit if I know well.

    You should put that in class somewhere and call the delete accordingly when program is exiting

    class Game
    {
    Cell *pCells;

    ~Game()
    {
    if( pCells ) delete pCells
    }
    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Simple class, delete exception

    Run the debugger and see what line is causing the error. Is it possible it's your GameObject destructor that's crashing?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Simple class, delete exception

    Quote Originally Posted by streamer View Post
    You don't have any problem.

    However you should write

    if(o1)
    {
    delete o1;
    o1 = NULL;
    }

    Possibly you are not deleting Cell class properly,

    If you have ie defined some variable globally, as
    Cell g_MYCELLS[10][10];

    destructor is called on the end of program exit if I know well.

    You should put that in class somewhere and call the delete accordingly when program is exiting

    class Game
    {
    Cell *pCells;

    ~Game()
    {
    if( pCells ) delete pCells
    }
    }
    He's unconditionally setting them in the constructor.
    Calling delete on a null pointer is okay.
    There's no point in setting their value to NULL in a destructor because they won't be accessible again anyway.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Simple class, delete exception

    Did you really forget to implement or disable the copy constructor and copy assignment operator? Maybe the code that uses this class performs copying, leading to double deletion since the compiler generated copy constructor and copy assignment operator perform shallow copying.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Simple class, delete exception

    Quote Originally Posted by mikazo View Post
    I apologize if this is a very simple error that has slipped my mind, but does anyone see anything wrong with this class?
    This simple 2 line program messes up your Cell class:
    Code:
    int main()
    {
       Cell a;
       Cell b = a;
    }
    The problem is that your class will go haywire if it's copied or assigned. You either have to disable these operations, or code a user-defined copy constructor and assignment operator.

    Before coding any class, you must be aware of these issues of copying and assignment, and handle them accordingly. Don't leave it as an afterthought.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple class, delete exception

    Quote Originally Posted by Paul McKenzie View Post
    You either have to disable these operations, or code a user-defined copy constructor and assignment operator.
    Or not do silly allocation of variables using naked/raw pointers in the first place.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Simple class, delete exception

    Quote Originally Posted by TheCPUWizard View Post
    Or not do silly allocation of variables using naked/raw pointers in the first place.
    Assuming this isn't a hypothetical example, that's the obvious answer.

  10. #10
    Join Date
    Feb 2009
    Posts
    9

    Re: Simple class, delete exception

    Ok I need to rescue my credibility here. I know exactly what copy and assignment constructors are and how they work and why you need them. I'm not here for a programming lesson. I'm in second year computer science and would hope that all second year computer science students know what copy and assignment constructors are by now.

    I have just just started this project (hence the very scantily-clad classes). In my main(), the only thing I do is create an array of Cell classes, then delete them. Nothing else happens yet. The GameObject class also has barely any code in it.

    All I want to know is, why can't I initialize a Cell containing a private pointer to a GameObject, then delete the Cell and the GameObject?

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Simple class, delete exception

    Have you taken the suggestion of a couple of us and used the debugger? What did it tell you?

  12. #12
    Join Date
    Feb 2009
    Posts
    42

    Re: Simple class, delete exception

    Quote Originally Posted by mikazo View Post
    Ok I need to rescue my credibility here. I know exactly what copy and assignment constructors are and how they work and why you need them. I'm not here for a programming lesson. I'm in second year computer science and would hope that all second year computer science students know what copy and assignment constructors are by now.

    I have just just started this project (hence the very scantily-clad classes). In my main(), the only thing I do is create an array of Cell classes, then delete them. Nothing else happens yet. The GameObject class also has barely any code in it.

    All I want to know is, why can't I initialize a Cell containing a private pointer to a GameObject, then delete the Cell and the GameObject?
    The problem is not in code you had posted. Problem is somewhere else in the program. Without some other code we can't help. Just tip what MAY be the problem.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Simple class, delete exception

    Quote Originally Posted by mikazo View Post
    Ok I need to rescue my credibility here. I know exactly what copy and assignment constructors are and how they work and why you need them. I'm not here for a programming lesson.
    The only code you posted shows a class that lacks copy constructor and assignment operator. We are left to conclude that there is a high possibility that this is what's causing the problem. If you did have all of those things, then post them. If you did not have those things, regardless if you know what they are or not, then the points that myself and others are making are valid -- a lack of a copy constructor and assignment operator will cause the issues you stated.
    In my main(),
    Please post what you are doing in main(). Do not describe it -- post exactly what it consists of.
    The GameObject class also has barely any code in it.
    Programming is an exact science -- there is no such thing as "barely". Post exactly what GameObject is.
    All I want to know is, why can't I initialize a Cell containing a private pointer to a GameObject, then delete the Cell and the GameObject?
    Provide the missing code, and you will get an answer. Don't provide the code, and we can keep guessing from here to who knows...

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Feb 2009
    Posts
    9

    Re: Simple class, delete exception

    Code:
    class GameObject {
    public:
    	GameObject() {
    	}
    	~GameObject() {
    	}
    private:
    	int a;
    };
    Code:
    class Cell {
    public:
    	Cell() {
    		o1 = new GameObject();
    		o2 = new GameObject();
    		o3 = new GameObject();
    		o4 = new GameObject();
    	}
    	~Cell() {
    		delete o1;
    		delete o2;
    		delete o3;
    		delete o4;
    	}
    private:
    	GameObject * o1;
    	GameObject * o2;
    	GameObject * o3;
    	GameObject * o4;
    };
    Code:
    Cell * grid[50][50];
    for (int i=0; i<50; i++) {
    	for (int j=0; j<50; j++)
    		grid[i][j] = new Cell();
    }
    for (int i=0; i<50; i++) {
    	for (int j=0; j<50; j++)
    		delete grid[i][j];
    }
    Did you really forget to implement or disable the copy constructor and copy assignment operator?
    My apologies for taking offence, but the way it was worded implied that I had no idea what I was doing.

    I used the debugger, and apparently in the line _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)); in dbgdel.cpp pHead causes the problem, having an access violation/null value.

    Let me know if anything else is needed.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Simple class, delete exception

    I am running the following program in Visual Studio 2005.
    Code:
    class GameObject {
    public:
        GameObject() {
        }
        ~GameObject() {
        }
    private:
        int a;
    };
    
    class Cell {
    public:
        Cell() {
            o1 = new GameObject();
            o2 = new GameObject();
            o3 = new GameObject();
            o4 = new GameObject();
        }
        ~Cell() {
            delete o1;
            delete o2;
            delete o3;
            delete o4;
        }
    private:
        GameObject * o1;
        GameObject * o2;
        GameObject * o3;
        GameObject * o4;
    };
    
    int main()
    {
        typedef Cell *CELLPTR;
        CELLPTR grid[50][50];
        for (int i=0; i<50; i++) {
            for (int j=0; j<50; j++)
                grid[i][j] = new Cell();
        }
    
        for (int i=0; i<50; i++) {
            for (int j=0; j<50; j++)
                delete grid[i][j];
        }
    }
    There were no errors when running the program.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured