2

I am having the error "Debug Assertion Failed" at the run time with the code below. I compiled the code with MVC++ 2010, in debug mode when I click on retry on the error window it directs me to the file dbgdel.cpp at the line

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

When I comment the line

delete result;

in the code, the error disappears...

I would appreciate your help to understand the problem. Sould I delete the pointer result, new is used to create it with clone method (Sorry I can't post the full code and it is too big)

Thanks for your help

/***CODE****/



########

File: DrawShapeBase.cpp 
...
...

Base* DrawShapeBase::SetOutput(Base* data)
{
   return (data->clone());
}


....
Base* DrawShapeBase::Draw(Base* data){
    Base* result = setOutput(data); 
        ...
    ...
    return result; 

}

/*##############*/
File: Derived.cpp

...
...

Base* Derived::clone()
{
    Base* b = new Derived(*this);
    return b;
}

...
...

/*##############*/
File Base.h
public:

    Base();
    virtual ~Base();
    virtual Base* clone(void) = 0;
....


/*##############*/
main.cpp 


...
...
Base* data = new Derived();
DrawBase* dr = new DrawShapeBase();
Base* result = dr->Draw(data);

delete data;
delete result;
delete dr; 
...
loisir2022
  • 163
  • 2
  • 11
  • The bug is most likely in code not shown. Though ugly (why aren't you using some kind of smart pointer and RAII?) the code looks okay. – David Schwartz May 04 '13 at 19:09
  • I will use smart pointers but I would like first to make this code works – loisir2022 May 04 '13 at 19:17
  • Virtual destructors might help, but that's just a guess. – David Schwartz May 04 '13 at 19:19
  • I used virtual destructors for all the classes that contain virtual functions but it doesn't work (got the same error). – loisir2022 May 04 '13 at 19:22
  • Your base class has a virtual `clone` function. Your code above shows a non-virtual destructor. – David Schwartz May 04 '13 at 19:34
  • Thanks. I've just updated the post but in my local code it was declared virtual – loisir2022 May 04 '13 at 19:41
  • Does `Derived` or `Base` have any dynamically allocated members? – hmjd May 04 '13 at 19:43
  • @hmjd The return of the clone in the Derived class is created with new. – loisir2022 May 04 '13 at 19:50
  • @loisir1976, yes. But does `Derived` contain any members that are dynamically allocated? – hmjd May 04 '13 at 19:55
  • @hmjd no, Derived has no members that are dynamically allocated, but it has a function that allocate dynamically a member inhereted from the Base class. This member is then deleted (with delete) in the destructor of the Base. – loisir2022 May 04 '13 at 20:09
  • @loisir1976, and in the copy constructor is that member correctly copied? – hmjd May 04 '13 at 20:10
  • `setOuput` != `SetOutput` – Oktalist May 04 '13 at 20:11
  • @hmjd I used the implicit copy constructor. I didn't implement one explicitly in the code. Is this a problem? Thanks – loisir2022 May 04 '13 at 20:19
  • @Oktalist Thanks I corrected this error in the post. In my local code it is correct. – loisir2022 May 04 '13 at 20:21
  • 3
    @loisir1976, yes. It means that two objects now point to the same dynamically allocated object. When one of those two objects is destructed it destroys that dynamically allocated object, leaving the other object with a dangling pointer which it eventually tries to `delete` causing the error you are seeing. See http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – hmjd May 04 '13 at 20:21

0 Answers0