c# - C++ .NET Wrapper: Attempted to read or write protected memory. This is often an indication that other memory is corrupt -


i having c++ .net wrapper unmanged mfc dll. wrapper used vb.net dll, called c# code. during runtime wrapper throws exception of attempted read or write protected memory.

system.accessviolationexception: attempted read or write protected memory.  indication other memory corrupt 

it seems occur randomly in "while" loop. throws @ beginning, in middle , nothing thrown.

how works: program need mfc dll. have wrapper.dll(c++) , myvbdll.dll(vb.net) referred program. added mfc dll content because it's not valid com component. how works:

myprogramm.exe->myvbdll.dll->wrapper.dll->mymfc.dll->mymfcfunction

info : if set field = "whatsoever"; before call mywrappedfunction, error never thrown!!

update : after several changes, problem still occurs. @ time converting unicode string ansi. there might find ... cause when write text in string above, works, when tostring function used, not work.

can body tell why occurs.

part of program in c#(reading 5000 lines .csv file textfieldparser field) :

string[] fields; string field ; string temp = "";  textfieldparser parser = new textfieldparser(textbox_csv.text, system.text.encoding.utf8); parser.textfieldtype = fieldtype.delimited; parser.setdelimiters(";");  while (!parser.endofdata) {     fields = parser.readfields();     field = fields[0];     temp = field.toupper();     field = myvbdll.mywrappedfunction(ref temp, false); } 

part of vb.net dll, called c# program :

public class myvbdll  public declare auto function mywrappedfunction lib "mywrapper.dll" (byval name string, byval opt boolean) string  end class 

part of mfc wrapper, called vb.net dll (error not mfc dll sure):

typedef void (*myfunction)(cstring&, cstring&, byte); myfunction myfunction;  lpwstr _stdcall mywrappedfunction(lpwstr valinput, byte opt) {     hinstance glibtestdll=null;     cstring s_valinput(valinput);     cstring s_resultat;      glibtestdll = afxloadlibrary(text(".\\test.dll"));     if(glibtestdll == null)     {         messagebox(null, text("unable load test.dll"), text("error"),mb_ok | mb_iconinformation);         return null;     }      myfunction = (myfunction)getprocaddress(glibtestdll, "myfunction");     if (myfunction == null)     {         messagebox(null, text("can't find myfunction."), text("error"),mb_ok | mb_iconinformation);          return null;     }     //******************************************************************      s_resultat.lockbuffer();     s_resultat.format("%64c", ' ');     myfunction(s_valinput , s_resultat , opt);     s_resultat.releasebuffer();      s_resultat.lockbuffer();     s_resultat.trimright();     s_resultat.releasebuffer();      // cstring unicode     uses_conversion;     s_resultat.lockbuffer();     lpwstr c_tmp= a2ole(s_resultat.getbuffer(s_resultat.getlength()));     s_resultat.releasebuffer();      afxfreelibrary(glibtestdll);      lpwstr c_resultat=c_tmp;     //******************************************************************      return c_resultat;  } 

lpwstr c_tmp= a2ole(s_resultat.getbuffer(s_resultat.getlength())); 

this very serious bug in c++ code. returning address of local variable. buffer created a2ole(). invokes undefined behavior in c++ code. tends work accident when call function c++ code, have odds there won't function call overwrites stack address local variable used be. odds turn 0 when pinvoke function, stack address gets obliterated pinvoke marshaller function calls. if doesn't cause crash pinvoke marshaller make sure crashes when tries release string cotaskmemfree().

you have fix c++ code first. , no, copying pointer c_resultat not fix.

do note there isn't point in trying rescue function. doesn't can't in c#. write [dllimport] attribute "myfunction" function.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -