使用 DLL 代码时堆损坏
我有一些代码需要放入公共库 dll 中。这段代码是一个 CalibrationFileData
类,当它作为当前项目的一部分构建时,工作得非常好。但是,如果 CalibrationFileData
构建在公共库中,则程序会崩溃,并出现堆损坏。
我已确保所有分配和释放都发生在类内,并使用适当的访问器等。但是,问题不会消失。为了以防万一,我有时会传递成对的向量,绝对不是普通的旧数据,但向量操作仅通过访问器发生,因此不应该在模块之间发生任何分配。
我缺少什么吗?
编辑:向量如下:
std::vector<std::pair<CvPoint2D32f, CvPoint3D32f>>* extrinsicCorrespondences;
std::vector<int>* pointsPerImage;
我不需要担心深层复制,因为它们不是堆分配的,对吗?顺便说一句,我尝试使用指向向量的指针(如上所述)来回避这个问题,但无论如何它都没有什么区别。
I have some code that I need to place in a common library dll. This code, a class CalibrationFileData
, works perfectly fine when it's built as part of the current project. However, if CalibrationFileData
is built in the common library, the program crashes, mentioning heap corruptions.
I have made sure that all allocations and deallocations occur within the class, with appropriate accessors, etc. Still, the problem won't go away. Just in case it makes any difference, I am sometimes passing vectors of pairs, definitely not plain old data, but the vector manipulation only occurs through the accessors, so there shouldn't be any allocation happening across modules.
Anything I'm missing?
Edit: The vectors are these:
std::vector<std::pair<CvPoint2D32f, CvPoint3D32f>>* extrinsicCorrespondences;
std::vector<int>* pointsPerImage;
I shouldn't need to worry about deep copies, since they're not heap allocated, right? Incidentally, I tried using pointers to vectors, as above, to sidestep the problem, but it didn't make a difference anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
检查库和可执行文件之间的编译标志是否匹配。例如,在 Windows 上,确保您使用相同的 C 运行时库 (CRT)(/MD 与 /MT)。检查来自链接器的警告。
Check the compile flags match between the library and the executable. For example, on Windows ensure you're using the same C Runtime Library (CRT) (/MD vs /MT). Check for warnings from the linker.
您确定当您在方法中获得向量对象内容的所有权时,您正在将它们深度复制到实例变量中吗?
Are you certain that when you take ownership of the contents of the
vector
objects, within your methods, you are deep-copying them into your instance variables?你应该检查向量对象内的深层副本,我认为它与深层副本有关
you should check deep copies inside vector object,it is pertain with deepcopy i think
两个项目中为 _SECURE_SCL 定义的值是否不同?
Could it be different values defined for _SECURE_SCL in the two projects ?
您可能错过了客户端代码的某些部分,这些代码试图释放您的 DLL 分配的内存,反之亦然。
也许最简单的事情是确保客户端和 DLL 使用相同的内存分配器。不仅是同类,而且是分配器的相同实际“实例”。在 Visual C++ 上,这可以通过客户端和 DLL 使用“多线程调试 DLL (/MDd)”或“多线程 DLL (/MD)”运行时库来最轻松地实现。
It's likely you missed some piece of the client's code that tries to deallocate a memory that your DLL allocated or vice versa.
Perhaps the easiest thing to do would be to ensure that both client and the DLL use the same memory allocator. Not just the same kind, but the same actual "instance" of the allocator. On Visual C++, this is most easily achieved by both client and DLL using a "Multi-threaded Debug DLL (/MDd)" or "Multi-threaded DLL (/MD)" runtime library.
在 Visual Studio 的命令行选项中删除此 _SECURE_SCL。大多数情况下,此崩溃是由细节之间的 _SECURE_SCL 不匹配引起的。更多详细信息可以在这里找到:http://kmdarshan.com/blog/?p=3830
In the command line option, in visual studio remove this _SECURE_SCL. Mostly this crash is caused by _SECURE_SCL mismatch among the details. More details can be found here: http://kmdarshan.com/blog/?p=3830
您应该区分紧耦合和松散耦合
DLL
。紧密耦合
DLL
意味着如果是松散耦合
DLL
,那么以字符串为例,则:
最后,关于内存分配,那么:
参考
从函数返回时堆损坏在 dll 内
我可以吗将 std::string 传递给 DLL?
You should distinguish between tightly coupled and loosely coupled
DLL
s.Tightly coupled
DLL
s mean thatIn case of loosely coupled
DLL
s, thenWith reference, for example, to strings, then:
Finally, concerning memory allocation, then:
References
Heap corruption when returning from function inside a dll
Can I pass std::string to a DLL?