CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2001
    Posts
    16

    Found my problem

    I finally found the problem in my code.
    I posted earlier that when I debug into my program it keeps crashing on a "new" operator but it runs fine. Now I found the cause of the problem. It is because I tried to access an object that was already deleted. It took me couple of days to find this problem. I don't know how you guys do it but I used _CrtCheckMemory to check the heap in all suspected areas. Right after I tried to access the deleted object, the program crashed in _CrtCheckMemory().

    I still do not know why it runs fine and crashes when I debug it. Is the heap somehow different in the two cases?

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    That's one of the reasons why we keep going on about avoiding the use of naked pointers in your code. Wrap new and delete in the RAII idiom (resource acquisition is initialisation) - acquire a resource inthe class constructor and release it in the destructor. Use the STL containers, rather than newing memory for an array. Etcetera.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Sep 2001
    Posts
    16
    Is this a C++ exception? I was not able to catch anything.

    Here is the problem in my code:

    class SomeObj
    {
    int x;
    }

    class A
    {
    SomeObj *obj;
    }

    main
    {
    A a;
    SomeObj *newobj = new SomeObj;
    a.obj = newobj;

    // somewhere
    delete newobj;

    // Error! try to acess delete object
    (a.obj)->x = 1;
    }

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    It's not an exception: it's undefined behaviour. IOW you can't predict what will happen if you do it (most likely is that your program will crash spectacularly or that you will trample over memory, and THEN your program will crash spectacularly).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Jun 2002
    Posts
    1,417
    Thats also the reason to initialize all class objects to some known value, normally 0 or NULL in the class constructor and to reinitialize it after deleting a pointer. Makes it a lot easier to see the problem when you see the address of a pointer is 0.

  6. #6
    Join Date
    Aug 2002
    Location
    VA, USA
    Posts
    137
    This is how I would go about it:

    class SomeObj
    {
    public:
    SomeObj() : m_x(0) {}
    SomeObj(const int x) : m_x(m) {}

    protected:
    int m_x;
    }

    class A
    {
    public:
    A() : m_obj(new SomeObj) {}

    protected:
    std::auto_ptr<SomeObj> m_obj;
    }

    main
    {
    A a;
    }

    Regards, willchop

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    If you're going to use auto_ptr in your class, then you are going to have to write your own copy ctor and copy assignment operator (or, more likely, disable them both). The compiler generated function will almost certainly do exactly the wrong thing.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Aug 2002
    Location
    VA, USA
    Posts
    137
    A "deep copy" is also needed for classes with raw pointer member
    variables. One solution would be to use the boost::shared_ptr.

    Regards, willchop

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