在C ++ DLL和Cpp / CLI控制台项目之间传输对象的向量 [英] Transferring vector of objects between C++ DLL and Cpp/CLI console project
问题描述
我有一个C ++库应用程序与C ++服务器谈话,我正在创建一个我的自定义类对象的向量。但是我的Cpp / CLI控制台应用程序(它与本地C ++交互),当我尝试返回我的自定义类obj向量时,抛出内存冲突错误。
代码示例 - / p>
在我的本地C ++类中 -
std :: vector< a> ; GetStuff(int x)
{
- do stuff
std :: vector< a> vec;
A a;
vec.push_back(a);
--- push more A objs
return vec;
}
在我的Cpp / CLI类中
public void doStuff()
{
std :: vector< a> vec;
vec = m_nativeCpp-> GetStuff(4); //其中nativeCpp是nativecpp DLL中动态分配的类,应用程序在这里引发内存违例错误!
}
确切的错误讯息
< blockquote>
在CLIConsole.exe(这是我的控制台cpp / CLI项目)中发生类型为System.AccessViolationException的未处理异常
信息:尝试读取或写入受保护的内存。这通常表示其他内存已损坏。
本地代码在单独编译的单元中,如.dll。首先,担心的是使用不同分配器(新/删除)的本机代码,当它用/ MT编译或链接到另一个版本的CRT时,你会得到。
接下来要担心的是STL迭代器调试。您应该确保两个模块都使用_HAS_ITERATOR_DEBUGGING的相同设置进行编译。如果本地代码是使用版本模式构建的旧版本CRT构建的,则它们不会相同。
I have a C++ library app which talks to a C++ server and I am creating a vector of my custom class objects. But my Cpp/CLI console app(which interacts with native C++ ), throws a memory violation error when I try to return my custom class obj vector.
Code Sample -
In my native C++ class -
std::vector<a> GetStuff(int x)
{
-- do stuff
std::vector<a> vec;
A a;
vec.push_back(a);
--- push more A objs
return vec;
}
In my Cpp/CLI class
public void doStuff()
{
std::vector<a> vec;
vec = m_nativeCpp->GetStuff(4); // where nativeCpp is a dynamically allocated class in nativecpp DLL, the app throws up a memory violation error here!
}
exact error message
An unhandled exception of type 'System.AccessViolationException' occurred in CLIConsole.exe -- which is my console cpp/CLI project
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I'll assume that the native code is in a separately compiled unit, like a .dll. First thing the worry about is the native code using a different allocator (new/delete), you'll get that when it is compiled with /MT or linked to another version of the CRT.
Next thing to worry about is STL iterator debugging. You should make sure both modules were compiled with the same setting for _HAS_ITERATOR_DEBUGGING. They won't be the same if the native code was built with an old version of the CRT of is the Release mode build.
这篇关于在C ++ DLL和Cpp / CLI控制台项目之间传输对象的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!