如何修复 c++ 中的 _CrtIValidHeapPoint(pUserData) 断言失败winforms应用程序
这是我第一次使用这样的网站,但我被严重卡住了。 下面的背景部分是为了完整性而包含的,因为我不确定其中描述的任何操作是否可能导致我当前面临的问题。
背景
我和一位同事在 Visual Studio 2010(完整版)的控制台应用程序(使用默认设置)中为大学作业编写了大量 C++ 代码。我们决定尝试一下 winforms 应用程序,因此在 Visual Studio 2010 中创建了一个应用程序,并从控制台应用程序导入了所有代码。我们面临以下问题:
- 需要在“stdafx.h”文件中添加包含内容,然后在所有实现文件(.cpp)中添加#include“stdafx.h”。
- 在我们使用 C++ 字符串 (std::string) 的地方,我们必须更改为托管系统字符串 (System::String^) 以实现 CLR 兼容性。
- 将“公共语言运行时支持”编译选项更改为 /clr 而不是 /clr:pure。这是解决我们在 boost::shared_ptr 中遇到的链接错误(LNK2028、LNK2019)所需要的......
在我们完成上述事情之后,我们的项目正在编译。
问题
当我们运行应用程序时,我们得到以下断言失败:
_CrtIsValidHeapPointer(pUserData)
此断言立即失败,似乎没有执行任何代码,因为当我在 main 开始处设置断点时,它们没有执行调试中达到(调用堆栈为空)。
我能够让应用程序运行的唯一方法是将 CLR 选项设置为 /clr:pure 并删除所有 boost 的使用。
我有3个问题:
- 代码正在执行吗?我知道在不执行代码的情况下断言可能失败听起来是不可能的。也许在 main 之前发生了什么?
- /clr 和 /clr:pure 选项的真正含义是什么?
- 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:
- Needed to add our includes in the "stdafx.h" file and then #include "stdafx.h" in all our implementation files (.cpp's).
- In places where we were using c++ strings (std::string), we had to change to managed system strings (System::String^) for CLR compatibility.
- 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:
- 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?
- What do the /clr and /clr:pure options really mean?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要尝试回答您的具体问题:
main()
是您的代码的入口点,而不是整个可执行文件。可能存在大量的样板初始化以及初始化所有全局静态对象。例如,在以下示例中,该函数在输入 main 之前运行:我对 /clr 的经验几乎为零,但我可以为您指出一些有用的参考。确保您了解 中适用于您的所有内容如何迁移到 /clr 和 /clr 设置。
我再次没有直接知识,但是此论坛帖子关于 boost::shared_ptr 和 这个关于 boost::thread 的问题似乎表明存在一些问题。
为了进一步诊断该问题,我会尝试以下操作:
pUserData
可能来自 CString 析构函数。确保所有字符串都迁移到托管系统字符串。To try and answer your specific questions:
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: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.
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:
pUserData
may be from a CString destructor. Make sure all strings are migrated to managed system strings.我知道已经很晚了,但我发现这个问题是因为我遇到了同样的问题。
查看我正在工作的另一个项目(不是我的),我发现问题来自文件:
opencv_ts248d.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:(I'm just including all them without focusing in what I need, except the one which results in the problem)
Hope it helps you