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

Posted on 2006-04-06
Last Modified: 2011-09-20
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...
Question by:pcad696
    LVL 12

    Expert Comment

    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.

    Author Comment

    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...
    LVL 16

    Expert Comment

    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.


    Author Comment

    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).

    Accepted Solution

    Closed, 500 points refunded.
    The Experts Exchange
    Community Support Moderator of all Ages

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now