dllexported 附加字符串已损坏
我有一个加载 dll 的 exe 文件。我都写了。
我正在 dll 导出一个返回 std::wstring 的函数 foo。
它所做的只是说
std::wstring blah = L"rgjwgfw";
return blah.append(L"hey");
在我导入 foo 的 exe 中,字符串已损坏。一旦我从调用 foo 的函数返回,我就会得到失败的断言。有时我收到一条消息,说 Windows 已在 foo.exe 中触发了断点。这可能是由于堆损坏等造成的......这也可能是由于用户在 foo.exe 获得焦点时按 f12 造成的。等等...
知道为什么会发生这种情况吗?如果我从 foo 中删除附加行并返回原始字符串,则不会发生这种情况,没有问题。
谢谢
i have an exe loading a dll. i wrote both.
i am dllexporting a function foo that returns a std::wstring.
all it does is say
std::wstring blah = L"rgjwgfw";
return blah.append(L"hey");
in the exe where i import foo, the string is corrupt. as soon as i return from the function where i call foo i get failed assertions. sometimes i get a message saying Windows has triggered a breakpoint in foo.exe. this may be due to a corruption of the heap, etc..... This may also be due to a user pressing f12 while foo.exe has focus. etc....
any idea why this happens? it doesn't happen if i remove the append line from foo and jsut return the original string, there are no problems.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个很常见的问题。您需要链接 DLL 和可执行文件才能使用 DLL 中的标准库。这样它们就可以共享标准库的公共副本和公共堆。否则,最终会导致可执行文件中的代码尝试使用一个堆,而 DLL 中的代码则使用单独的堆。当您跨越边界传递几乎任何使用动态分配的内容时,每个对象都假设对象中的动态分配的缓冲区来自其自己的堆。几乎任何操作不仅会损坏对象,还会损坏整个堆。
This is pretty common problem. You need to link both the DLL and the executable to use the standard library in a DLL. That way they share a common copy of the standard library a common heap. Otherwise, you end up with code in the executable trying to use one heap, and code in the DLL using a separate heap. When you pass almost anything that uses dynamic allocation across the boundary, each assumes the dynamically allocated buffer in the object came from its own heap. Almost any manipulation can not only corrupt the object, but also the entire heap.