How to return array of custom objects from native C++ class to cpp/CLI class.
-
Hi, I have a C++ library app which talks to a C++ server and I am creating a vectorje of my custom class objects. But my Cpp/CLI 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 GetStuff(int x) { -- do stuff std::vector vec; A a; vec.push_back(a); --- push more A objs return vec; } In my Cpp/CLI class public void doStuff() { std::vector 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! } Anything I am missing here ?What should be the ideal way to return such an array - Regards Amit
-
Hi, I have a C++ library app which talks to a C++ server and I am creating a vectorje of my custom class objects. But my Cpp/CLI 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 GetStuff(int x) { -- do stuff std::vector vec; A a; vec.push_back(a); --- push more A objs return vec; } In my Cpp/CLI class public void doStuff() { std::vector 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! } Anything I am missing here ?What should be the ideal way to return such an array - Regards Amit
Type
A
is created on stack and this may be the problem. Have you tried to allocate it on heap and letvector
hold a pointer to it. I believe that will solve the issue. I haven't tried this, but a wild guess.Member 5703905 wrote:
throws up a memory violation error
What is the exact error message? :)
Navaneeth How to use google | Ask smart questions
-
Type
A
is created on stack and this may be the problem. Have you tried to allocate it on heap and letvector
hold a pointer to it. I believe that will solve the issue. I haven't tried this, but a wild guess.Member 5703905 wrote:
throws up a memory violation error
What is the exact error message? :)
Navaneeth How to use google | Ask smart questions
Okay, but I wanted to convey that nativeC++ DLL and Cpp/CLI DLL are in diff projects. Not sure if it is a nice idea to return a ptr from unmanaged project to managed cpp/cli project.
N a v a n e e t h wrote:
What is the exact error message?
An unhandled exception of type 'System.AccessViolationException' occurred in CLIConsole.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Thanks Amit
-
Okay, but I wanted to convey that nativeC++ DLL and Cpp/CLI DLL are in diff projects. Not sure if it is a nice idea to return a ptr from unmanaged project to managed cpp/cli project.
N a v a n e e t h wrote:
What is the exact error message?
An unhandled exception of type 'System.AccessViolationException' occurred in CLIConsole.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Thanks Amit
Member 5703905 wrote:
Not sure if it is a nice idea to return a ptr from unmanaged project to managed cpp/cli project.
It's fine in C++/CLI. Native objects will be on the native heap, managed objects will be on the managed heap. If you need to hold the pointer in a managed object, you can use the IntPtr type. If you expect to allocate the native object in one DLL and free it in another DLL, you'll want to make sure both DLLs are sharing the same CRT library (DLL).
Mark Salsbery Microsoft MVP - Visual C++ :java: