visual c++ - C++ delete statement throws Heap corrupted exception in .net 4.5 process -
earlier had .net client application (console application) target framework set 4.0. client code looks ,
comproxycustomclass proxy = new comproxycustomclass(); proxy.loadfieldservicecomponent("abc", 3);
where .net client make use of c++ com type comproxycustom via reference rcw interop dll generated importing tlb of c++ com project. implementation of loadfieldservicecomponent method looks ,
stdmethodimp ccomproxycustom::loadfieldservicecomponent(bstr fscname, int version) { hresult hr = s_ok; ilocalemanager *localemanager = null; hr = cocreateinstance(clsid_localemanager, null , clsctx_all , iid_ilocalemanager, (void**)&localemanager); delete(localemanager); localemanager = null; return hr; }
again here ilocalemanager .net type gets used in c++ project exporting tlb .net project , including/importing here in c++ com project.
#import "someinterop.tlb" no_namespace named_guids
till here alright.
here comes problem , make use of features provided .net 4.5 (system.io.compression) moved c# client project target .net 4.5 version(earlier .net 4.0). after doing migration , started getting heap corrupted error @ statement delete(localemanager).
now why error when particular statement (delete) gets executed in .net process targets 4.5 , why not in 4.0 , there change in how memory should freed in c++ code if .net application loads c++ code targets 4.5 framework version ? , how clear memory of localemanager if delete cant used ?
you're using com, should call release() on localemanager instead of deleting it.
if (localemanager) localemanager->release();
com use reference count manage object life cycle.
as c++ delete key word, has know type of pointer, relies on c++ rtti works; com, has different rtti implementation. com object has different layout structure c++ object.
as said, delete works earlier, doesn't. deleting com pointer corruptthe heap, however, won't crash @ line of delete statement; behavior is: program sometime crashes, sometime not.
Comments
Post a Comment