如何修复 c++ 中的 _CrtIValidHeapPoint(pUserData) 断言失败winforms应用程序

发布于 2024-12-10 17:55:59 字数 1090 浏览 0 评论 0原文

这是我第一次使用这样的网站,但我被严重卡住了。 下面的背景部分是为了完整性而包含的,因为我不确定其中描述的任何操作是否可能导致我当前面临的问题。

背景

我和一位同事在 Visual Studio 2010(完整版)的控​​制台应用程序(使用默认设置)中为大学作业编写了大量 C++ 代码。我们决定尝试一下 winforms 应用程序,因此在 Visual Studio 2010 中创建了一个应用程序,并从控制台应用程序导入了所有代码。我们面临以下问题:

  1. 需要在“stdafx.h”文件中添加包含内容,然后在所有实现文件(.cpp)中添加#include“stdafx.h”。
  2. 在我们使用 C++ 字符串 (std::string) 的地方,我们必须更改为托管系统字符串 (System::String^) 以实现 CLR 兼容性。
  3. 将“公共语言运行时支持”编译选项更改为 /clr 而不是 /clr:pure。这是解决我们在 boost::shared_ptr 中遇到的链接错误(LNK2028、LNK2019)所需要的......

在我们完成上述事情之后,我们的项目正在编译。

问题

当我们运行应用程序时,我们得到以下断言失败:

_CrtIsValidHeapPointer(pUserData)

此断言立即失败,似乎没有执行任何代码,因为当我在 main 开始处设置断点时,它们没有执行调试中达到(调用堆栈为空)。

我能够让应用程序运行的唯一方法是将 CLR 选项设置为 /clr:pure 并删除所有 boost 的使用。

我有3个问题:

  1. 代码正在执行吗?我知道在不执行代码的情况下断言可能失败听起来是不可能的。也许在 main 之前发生了什么?
  2. /clr 和 /clr:pure 选项的真正含义是什么?
  3. boost 库可以编译为 clr 兼容代码吗?如果是这样,什么可能会导致链接问题?

谢谢大家的宝贵时间,我没有发布任何代码,因为我真的不知道该发布什么......我不知道问题发生在哪里。如果有人对发布什么代码有建议,我愿意接受。

问候 布拉德

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-12-17 17:55:59

要尝试回答您的具体问题:

  1. main()您的代码的入口点,而不是整个可执行文件。可能存在大量的样板初始化以及初始化所有全局静态对象。例如,在以下示例中,该函数在输入 main 之前运行:

     int SomeGlobal = SomeFunction();
    
       int main(空)
       {
       ...
       }
    
  2. 我对 /clr 的经验几乎为零,但我可以为您指出一些有用的参考。确保您了解 中适用于您的所有内容如何迁移到 /clr/clr 设置

  3. 我再次没有直接知识,但是此论坛帖子关于 boost::shared_ptr这个关于 boost::thread 的问题似乎表明存在一些问题。

为了进一步诊断该问题,我会尝试以下操作:

  • 从一个空的或“hello world”CLR 项目开始,以确保它可以编译并运行。
  • 使用 boost 尝试一个简单的示例,看看它是否有效或是否会重复该问题。
  • 尝试清理并完全重建您的应用程序。
  • pUserData 可能来自 CString 析构函数。确保所有字符串都迁移到托管系统字符串。
  • 如果您的简单示例有效,那么只需添加示例直到重复问题,或者从应用程序中删除内容直到问题消失。

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.
沙沙粒小 2024-12-17 17:55:59

我知道已经很晚了,但我发现这个问题是因为我遇到了同样的问题。

查看我正在工作的另一个项目(不是我的),我发现问题来自文件:opencv_ts248d.lib。如果您从依赖项中删除它,它会正常工作(我不知道为什么,猜测问题出在属于它的函数中),因此您将得到以下结果:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文