Bradley Bow
Bradley Bow

Reputation: 31

How to fix _CrtIValidHeapPoint(pUserData) assertion failure in c++ winforms application

This is the first time I have ever used a site like this, but I am significantly stuck. The background section that follows is included for completeness as I am not sure if any of the actions described within it may have contributed to the current problem I am facing.

Background

A colleague and I had written a whole lot of c++ code in a console application (with default settings) in visual studio 2010 (full version) for a university assignment. We decided that we wanted to try our hand at a winforms application and so created one in visual studio 2010 and imported all our code from the console application. We faced the following issues:

  1. Needed to add our includes in the "stdafx.h" file and then #include "stdafx.h" in all our implementation files (.cpp's).
  2. In places where we were using c++ strings (std::string), we had to change to managed system strings (System::String^) for CLR compatibility.
  3. Changed the "Common language runtime support" compile option to /clr instead of /clr:pure. This was needed to solve linking errors (LNK2028, LNK2019) we had with boost::shared_ptr ...

After we had done the above things our project was compiling.

The Problem

When we run the application we get the following assertion failure:

_CrtIsValidHeapPointer(pUserData)

This assertion fails immediately, seemingly without any code being executed, as when I set breakpoints at the start of main they are not reached in debugging (call stack is empty).

The only way I have been able to get the application to run is by setting the CLR option to /clr:pure and removing all usage of boost.

I have 3 questions:

  1. Is code being executed? I know it sounds impossible that an assertion could be failing without code being executed. Is there something that happens before main perhaps?
  2. What do the /clr and /clr:pure options really mean?
  3. Can boost libraries be compiled to clr compatible code? If so, what may cause linking issues?

Thank you all for your time, I have not posted any code because I literally don't know what to post...I have no idea where the issue is occurring. If anyone has suggestions for what code to post, I am open to them.

Regards Brad

Upvotes: 3

Views: 1563

Answers (2)

Btc Sources
Btc Sources

Reputation: 2061

I know it's quite late, but I found this question because I had the same problem.

Looking in another project I've working (not mine), I've found that the problem comes from the file: opencv_ts248d.lib. If you delete it from your depencencies, it'll work fine (I don't know why, guess the problem is in a function which belongs to it), so you'll have the following ones:

opencv_calib3d248d.lib
opencv_contrib248d.lib
opencv_core248d.lib
opencv_features2d248d.lib
opencv_flann248d.lib
opencv_gpu248d.lib
opencv_highgui248d.lib
opencv_imgproc248d.lib
opencv_legacy248d.lib
opencv_ml248d.lib
opencv_nonfree248d.lib
opencv_objdetect248d.lib
opencv_ocl248d.lib
opencv_photo248d.lib
opencv_stitching248d.lib
opencv_superres248d.lib
opencv_video248d.lib
opencv_videostab248d.lib

(I'm just including all them without focusing in what I need, except the one which results in the problem)

Hope it helps you

Upvotes: 0

uesp
uesp

Reputation: 6204

To try and answer your specific questions:

  1. main() is the entry point to your code and not the entire executable. There may be a large amount of boilerplate initialization as well as initializing all global static objects. For example, in the follow example the the function is run before main is entered:

       int SomeGlobal = SomeFunction();
    
       int main (void)
       {
       ...
       }
    
  2. I have near zero experience with /clr but I can point you at a few useful references. Make sure you understand everything that applies to you in How To Migrate to /clr and the /clr settings.

  3. I again have no direct knowledge but this forum post on boost::shared_ptr and this SO question on boost::thread seem to indicate there are some issues.

To further diagnose the issue I would try a few things:

  • Start with an empty or "hello world" CLR project to make sure it compiles and runs.
  • Try a simple sample using boost and see if that works at all or duplicates the issue.
  • Try cleaning and doing a complete rebuild of your application.
  • The pUserData may be from a CString destructor. Make sure all strings are migrated to managed system strings.
  • If you simple samples work then it is a matter of adding to the samples until you duplicate the issue, or removing stuff from your application until the issue goes away.

Upvotes: 1

Related Questions