Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 13645
  • Last Modified:

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"



All my cpp files and the project are using the "Multi-threaded Debug DLL (/MDd)" runtime library.
I must be missing something simple...
1 Solution
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.
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...
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.

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:


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).
Closed, 500 points refunded.
The Experts Exchange
Community Support Moderator of all Ages

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now