c# - Attempted to read or write protected memory. This is often an indication that other memory is corrupt -
i'm getting error while trying pass array of string c# c++. error appears sometimes, not always.
declaration in c#
[dllimport(reader_dll, callingconvention = callingconvention.stdcall, charset = charset.unicode)] public static extern void installedrooms(string[] rooms,int length);
in c++
void installedrooms(wchar_t const* const* values, int length); void detectorimpl::installedrooms(wchar_t const* const* values, int length) { logscope scope(log_, log::level::full, _t("detectorimpl::installedrooms"),this); std::vector<std::wstring> vstr(values, values + length); m_installedrooms=vstr; }
how invoked c#?
//list<string> installedrooms = new list<string>(); //installedrooms.add("r1"); //installedrooms.add("r1"); etc nativedetectorentrypoint.installedrooms(installedrooms.toarray(),installedrooms.count);
error raised at
attempted read or write protected memory. indication other memory corrupt. @ mh_detectorwrapper.nativedetectorentrypoint.installedrooms(string[] rooms, int32 length)
any help appreciated
this guess since error intermittent believe memory issue related string
array installedrooms
.
if not mark managed object using fixed
keyword, gc
may alter location of object @ time. thus, when seek access related memory location unmanaged code, may throw error.
you may seek following;
list<string> installedrooms = new list<string>(); installedrooms.add("r1"); installedrooms.add("r2"); string[] roomsarray = installedrooms.toarray(); fixed (char* p = roomsarray) { nativedetectorentrypoint.installedrooms(p, roomsarray.count); }
c# c++ dllimport access-violation
No comments:
Post a Comment