c++ - P/Invoke: Memory corruption with pointer -
c++ - P/Invoke: Memory corruption with pointer -
i'm wrapping part of fbx sdk (closed, public api) mono (so com, cli aren't options) , bunch of extern's, , going until had homecoming non-pointer instance. see here
the crucial point have homecoming c++ call. because don't know how you'd without pointer, returned such:
fbxapi fbxproperty* object_getfirstproperty(fbxobject* obj) { homecoming &obj->getfirstproperty(); }
..and it's not until seek next snippet "system.accessviolationexception : attempted read or write protected memory. indication other memory corrupt." message.
fbxapi const wchar_t* property_getname(fbxproperty* prop) { int c = prop->getsrcpropertycount(); homecoming l"test"; }
if utilize identical code using same calls in c++, it's fine. i've done ~20 function calls in same manner without having "pointerfy" it, , they're fine too, don't think dllimport's blame. if reference blame, how else do it? certainly don't store global static reference somewhere because called api?
any help appreciated, c/c++ , explicit way handles memory new me.
i assume programme crashing because property getting pointer no longer exist. allow me clarify , start dissecting following:
fbxapi fbxproperty* object_getfirstproperty(fbxobject* obj) { homecoming &obj->getfirstproperty(); }
i looked documentation of fbx, , fbxobject::getfirstproperty()
has homecoming type of fbxproperty
. notice homecoming value isn't pointer or reference? means called 'auto-variable', or in case 'temporary'. kind of object lasts until leave scope, in case object_getfirstproperty()
of wrapper. after that, object cleaned , removed memory stack. fbxobject::getfirstproperty()
gives re-create of property, not actual reference. internally might different, wrapper concerned property object itself, not it's content.
so doing pointer address no longer valid later on when pass property_getname()
.
c++ behaves differently c# in regards object lifetime. object in c# called myobj
can thought of c++ pointer type myobject*
- it's reference value. in c# have value-types struct
, forth, equivalent c++ auto-variable. auto-variables destroyed when lifetime scope left.
what you'd have overcome problem save object fbxobject::getfirstproperty()
directly, , not pointer it. you'd have marshall object proper .net class it's contents not lost.
alternatively, allocate dynamic memory , re-create object fbxobject::getfirstpoperty()
there, , homecoming pointer own memory. of course of study you'd have delete memory later on manually. here simple example:
fbxapi fbxproperty* object_getfirstproperty(fbxobject* obj) { // allocate custom memory. char* mymem = new char[sizeof(fbxproperty)]; // re-create property's content there. std::memcpy(mymem, &obj->getfirstproperty(), sizeof(fbxproperty)); // homecoming custom memory address. homecoming reinterpret_cast<fbxproperty*>(mymem); }
this should solve memory corruption issue. in c++ you'd have free memory manually when finished property doing ths:
fbxapi void property_free(fbxproperty* prop) { // free allocated memory delete[] prop; }
but effort may cause other problems, depending on how actual fbxproperty
handles it's info inside. creating re-create of object, sure, if original temporaty/auto-variable deletes of import memory upon destruction, have similar issus ones have now.
if witty write real wrapper classes every fbx type require , marshall whole class type instead of generating separete c functions have p/invoke every time want value or property.
c++ mono pinvoke
Comments
Post a Comment