.net - Non-managed referenced code strangely, "magically" changes its state for no reason when wrapped in managed -


i have strange problem:

my unmanaged third-party library has class, let's call foo has method bar() returns object of type bar, like:

foo* foo = new foo(); bar bar = foo -> bar(); 

now, bar has method supposed return true when obtained via above means. in unmanaged code, works desired:

foo* foo = new foo(); bar bar = foo -> bar(); // yes, bar() returns object, not pointer bool b = bar.shouldbetrue(); // b true 

now, wrote managed wrapper foo , bar simple:

managed.h:

namespace managed {      public ref class managedbar {     private:         thirdparty::bar* _delegate;      public:         managedbar(thirdparty::bar* delegate);          ~managedbar();          bool shouldbetrue();     };      public ref class managedfoo {     private:         thirdparty::foo* _delegate;      public:         managedfoo();          ~managedfoo();          managedbar^ bar();     }; } 

managedbar.cpp (includes stripped):

namespace managed {      managedbar::managedbar(thirdparty::bar* delegate) {         _delegate = delegate;     }      managedbar::~managedbar() {         delete _delegate;     }      bool managedbar::shouldbetrue() {         return _delegate -> shouldbetrue();     } } 

managedfoo.cpp:

namespace managed {      managedfoo::managedfoo() {         _delegate = new thirdparty::foo();     }      managedfoo::~managedfoo() {         delete _delegate;     }      managedbar^ managedfoo:bar() {         thirdparty::bar tpb = delegate -> bar();         //for test/debugging:         bool b = tpb.shouldbetrue(); // b true         return gcnew managedbar(&tpb);     } } 

now, when call following in vb.net (in unit test):

imports managed  <testclass()> public class mytest      <testmethod()>     public sub testbarreturnstrue()         dim f managedfoo = new managedfoo()         dim b managedbar = f.bar()         assert.istrue(b.shouldbetrue())     end sub  end class 

but assert fails because false. when step managedbar, _delegate -> shouldbetrue() called without errors. behaviour strange. did wrong wrapping, or have ask supplier of third-party dll advice?

the problem in:

    managedbar^ managedfoo:bar() {     thirdparty::bar tpb = delegate -> bar();     //for test/debugging:     bool b = tpb.shouldbetrue(); // b true     return gcnew managedbar(&tpb); } 

tpb destroyed after function call. should allocate tpb on heap(you need manually free it), save somewhere(as member) or pass value managedbar.

hope you.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -