Check to see if object exists before attempting to delete it

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rickbird
    New Member
    • Oct 2008
    • 1

    #1

    Check to see if object exists before attempting to delete it

    I have a composition object that is written in C++. The container class deletes the dynamic object in its destructor. However, if someone creates the object in the main and passes it to the container class, they will probably try to delete the dynamic object in their code. How do I test to see if the object exists before I delete it? The code below crashes since the dynamic Engine was deleted in the main. However, I need to clean up the memory in the class in case the Vehicle is created with the default constructor and the main() never uses the "new" keyword. Thanks!

    int main( ){
    // create composition object
    Engine * engine = new Engine( );
    Vehicle vehicle( engine );

    // clean the memory
    delete engine;
    }

    class Vehicle{
    public:
    // constructors
    Vehicle( ){
    engine = new Engine( );
    }
    Vehicle( Engine * engine ){
    this->engine = engine;
    }

    // destructor
    ~Vehicle( ){
    delete engine;
    }
    private:
    Engine * engine;
    };
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    No matter whether the default constructor is called or you use the 1 argument constructor, you are still assigning to an Engine* in Vehicle. Thus, you should be deleting engine only in Vehicle. Do not delete it in main() - when your Vehicle goes out of scope and the destructor is called, it will delete engine for you.

    Comment

    • boxfish
      Recognized Expert Contributor
      • Mar 2008
      • 469

      #3
      How about setting engine = NULL wherever you delete it and then only deleting it if engine != NULL?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by boxfish
        How about setting engine = NULL wherever you delete it and then only deleting it if engine != NULL?
        That wont work, setting the local variable in main to NULL will not effect the value of the engine member in Vehicle so the Vehicle destructor would still try to delete it and crash.

        Ganon's idea is right, once you have passed ownership of the engine to the Vehicle it is up to the Vehicle to delete it.

        Of course it might be better to just let Vehicle create its own engine all the time, then all the memory handling is nicely encapsulated by Vehicle. In the constructor that takes an engine * you would use that to initialise the engine created by Vehicle.

        Another option would be to use handles rather than pointers.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by Banfa
          Another option would be to use handles rather than pointers.
          In fact, this is the solution.

          Read this: http://bytes.com/forum/thread651599.html.

          Comment

          Working...