delete pointers, VC++, Debug Assertion Failled

Hello!
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 direct me to the file dbgdel.cpp at the line

 
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));


When I comment in the code the line
 
delete result;

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

Thanks for your help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


########

File: DrawShapeBase.cpp 
...
...

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


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

}

##############
File: Derived.cpp

...
...

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

...
...
##############
File Base.h
public:
  
	Base();
  
	~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; 
...
Last edited on
> Sorry I can't post the full code and it is too big
Try to create a minimal example.
At least post relevant parts
1
2
3
4
Base* result = dr->Draw(data);

delete data;
delete result;


Doesn't look like result was created with new. Why are you deleting it?

Unless Draw() returns a new'd pointer... but that seems extremely goofy if that's really what it does.

Did you mean to delete 'dr' instead of 'result'?


EDIT: also, use smart pointers to save yourself these kinds of headaches
Last edited on
Is the destructor of Base declared virtual?
Thanks for the replies!

@Disch you are right, Draw() uses a clone method that return a new pointer and then process the data and finally returns the pointer.

I use delete for dr, data, (both are 'new' pointers) and result, but it is the delete of result that cuase the problem.

@KRAkatau the destructor of Base is declared like this:
~Base();
Is it a problem?


@ne555 I added here more details

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


########

File: DrawShapeBase.cpp 
...
...

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


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

}

##############
File: Derived.cpp

...
...

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

...
...
##############
File Base.h
public:
  
	Base();
  
	~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; 
...



Thanks





Last edited on
Hello!

I updated the code: added more details, I hope that it will help
Yes, this is the problem. When you delete an object by pointer to a base class, in order for correct destructor to be called, the base-class destructor should be declared as virtual: virtual ~Base();

Most likely this causes your debug assertion. In Release production code this might lead to either memory leak or a crash.
@KRAkatou I have put in the classes that contains virtual functions virtual destructors (virtual ~class_name()) but I still getting the same error...
Use smart pointers, as suggested above.
This will solve most of the problems, if not all, and also helps you building better programs in an easier way.
@EssGeEich I will use smart pointers but I would like first to make this code works
> I use delete for dr, data, (both are 'new' pointers) and result,
¿is there something that you don't new/delete?
You do know that you can declare objects, ¿right?

Again, try to create a minimal example that does reproduce your issue.
Topic archived. No new replies allowed.