Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to return array of custom objects from native C++ class to cpp/CLI class.

How to return array of custom objects from native C++ class to cpp/CLI class.

Scheduled Pinned Locked Moved Managed C++/CLI
c++graphicssysadmindata-structuresperformance
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 5695619
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • U User 5695619

      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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Type A is created on stack and this may be the problem. Have you tried to allocate it on heap and let vector 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

      U 1 Reply Last reply
      0
      • N N a v a n e e t h

        Type A is created on stack and this may be the problem. Have you tried to allocate it on heap and let vector 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

        U Offline
        U Offline
        User 5695619
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • U User 5695619

          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

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups