Debug assertion failed: dbgdel.cpp line 52 _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

In a mixed C++ DLL that uses MFC (called from VB.NET) I have the narrowed down the problem to two lines of code:

      SimpleClass * sim = new SimpleClass;
      delete sim;

when stepping (or running) through the delete, it generates "Debug assertion failed: dbgdel.cpp line 52 _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"

I have also narrowed down SimpleClass to an empty class:

#include "StdAfx.h"
#include ".\simpleclass.h"

SimpleClass::SimpleClass(void)
{
}

SimpleClass::~SimpleClass(void)
{
}

All my cpp files and the project are using the "Multi-threaded Debug DLL (/MDd)" runtime library.
I must be missing something simple...
pcad696Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeev_devinCommented:
Nothing wrong in the thing that you have posted.
Problem lies somewhere else.
Most probably you are trying to delete some memory in destructor od SimpleClass that is already deleted.
Post some more details.
0
pcad696Author Commented:
The complete code of simpleclass is already there; the destructor does nothing, in fact it never even reaches the destructor if I put something in it.

THis code of course works in a regular environment. But when you put this regular C++ inside a DLL with some managed C++ (and MFC), the whole thing built as a class library (DLL), and call it from VB it simply crashes on the delete statement (again, does not go into the destructor).

I also noticed that if I do not delete the SimpleClass object (even when it contains some actual variables) VC++ does not complain of memory leaks.

I suspect this is some kind of problem with exactly what functions are called for "new" and "delete". Does a DLL have its own heap? what about when MFC is there? I know MFC has a special new operator? I am not too sure about the details of dynamic memory allocation in the context of a mixed-mode MFC class library...
0
PaulCaswellCommented:
Hi pcad696,

I'd guess either the 'new' failed without throwing an exception, (which depends on your compiler/system I believe) or your heap has been corrupted just prior to this code (if you're lucky). Sadly it might have been damaged for thousands of lines of code :-(. Get hold of a decent heap exerciser and try using that.

Paul
0
pcad696Author Commented:
I think I have solved my problem. I actually managed to reproduce it with a dummy solution, two C++ project, one Winform, one mixed-mode Class library with MFC, each with only a few lines of code. There was no other code to go wrong.

Because the DLL is mixed-mode, I had followed the detailed Microsoft advice at:

http://support.microsoft.com/?id=814472

and added msvcrt.lib to my link (in my usual debug config).

seems I needed to use msvcrtd.lib instead (probably obvious to anyone with the experience).
0
GranModCommented:
Closed, 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.