streetsoldier
streetsoldier

Reputation: 1319

Deleting an object in C++ deletes objects inside the deleted object?

I have a game class, cube class and a solution class. In my main function, I am creating a game object g and the game object has an array of cube type objects and a stack of solution type objects. In the main function, before exiting, I am deleting the object g by calling delete[] g;

I wanted to know if this will delete all the objects in the array and the stack as well? Or will I still have a memory leak?

Sample code:

int main(void)
{

Game* g = new Game();
//Do something like addCube by calling functions in game

delete g;
}

Game Constructor:

public: Game()    
    {    
        int nx;    
        cout<<"Enter the number of cubes in the game ";    
        cin>>nx;    
        this->numOfCubes=nx;    
        cubes = new Cube[nx];        
        this->ref=0;    
    }

Sample of game functions

    void Game::addCube()
{    
    if(ref<numOfCubes)
    {   
        cubes[ref].getSides();      
        ref++;   
        cubes[ref]->Sno = ref;    

    }
    else
        cout<<"All the cubes are already in the game"<<endl;
}

Upvotes: 1

Views: 2146

Answers (4)

Fred Larson
Fred Larson

Reputation: 62083

If you are talking about an array of actual objects, then yes, they will be destroyed properly. If you have an array of pointer, you'll have to delete each object (assuming each object was created with new).

EDIT:

Well, you have trouble right here:

int main(void)
{

Game* g = new Game();
//Do something like addCube by calling functions in game

delete[] g;
}

g is not pointing to an array, just a single instance. So delete [] g; is undefined behavior. It should be just delete g; Better yet, don't use dynamic allocation at all:

int main(void)
{

Game g;
//Do something like addCube by calling functions in game

// No delete necessary since you're using automatic allocation.
}

As for your cubes array, you'll have to delete the array itself, since it is created with new []. Be sure to use delete [] cubes;. But you are leaking cubes in addCube(), as pointed out by Blastfurnace. I think you're trying to do a dynamic array, in which case std::vector would be your best friend.

Upvotes: 3

Blastfurnace
Blastfurnace

Reputation: 18652

Whatever other problems you may have, your addCube() function is flawed. Comments added:

{
    Cube* c = new Cube(); // allocate a Cube object
    c->getSides();
    cubes[ref]=*c; // dereference pointer and store a copy of the object
    ref++;
    c->Sno = ref; // modify allocated Cube (not the same object as cubes[ref])
}
// pointer c goes out of scope and you leak the allocated memory

There's no reason to use new in this code.

Upvotes: 0

mah
mah

Reputation: 39807

Calling delete on an object will only recurse to delete the objects within it if that object's destructor manually does the delete on each of them. There is no automatic deletion or garbage collection in c++.

Upvotes: 0

Daniel Garrett
Daniel Garrett

Reputation: 121

No the inner objects will persist, but if you know you want to delete inner objects every time you can create a destructor method of the style ~ClassName() inside the the class definition, inside this destructor method, you can call destructor methods for each of the inner objects you wish to delete. Just remember that if these objects exist elsewhere in your code, you will get unexpected behavior or more likely a segfault.

Upvotes: 0

Related Questions