title: How to return by value from native function?
body: <p>I have the following C++ method compiled using Visual Studio 2017:</p>
  
  <pre><code>extern "C" __declspec( dllexport )
  Info* __stdcall GetInfo(InfoProvider* infoProvider)
  {
     static_assert(std::is_pod&lt;Info&gt;::value, "Must be Plain Old Data in order to be safely copied between DLL boundaries");
  
     Info info = new Info();
     Info-&gt;data1 = infoProvider-&gt;data1;
     Info-&gt;data2 = infoProvider-&gt;data2;
  
     return info;
  }
  </code></pre>
  
  <p>In Java code, it is mapped by Java Native Runtime using interface method with following signature:</p>
  
  <pre><code>Info GetInfo(Pointer infoProvider);
  
  final class Info extends Struct {
  
      public final Signed32 data1;
  
      public final Signed32 data2;
  
      public R2VInfo(final Runtime runtime) {
          super(runtime);
  
          data1 = new Signed32();
          data2 = new Signed32();
      }
  }
  </code></pre>
  
  <p>It works.</p>
  
  <p>The above C++ method causes memory leak, so I would like to change it to return result by value:</p>
  
  <pre><code>extern "C" __declspec( dllexport )
  Info __stdcall GetInfo(InfoProvider* infoProvider)
  {
     static_assert(std::is_pod&lt;Info&gt;::value, "Must be Plain Old Data in order to be safely copied between DLL boundaries");
  
     Info info{};
     Info.data1 = infoProvider-&gt;data1;
     Info.data2 = infoProvider-&gt;data2;
  
     return info;
  }
  </code></pre>
  
  <p>I use the same Java JNR mapping:</p>
  
  <pre><code>Info GetInfo(Pointer infoProvider);
  </code></pre>
  
  <p>But it does not work - Access Violation. Native method is invoked, but with some dandling pointer value.</p>
  
  <p>How to return by value in JNR?</p>