Click here to Skip to main content
15,887,945 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: About Hook InternetConnectA and HttpOpenRequestA modify URLs Pin
678474615-Aug-11 8:47
678474615-Aug-11 8:47 
QuestionHow to get Actual CFormView Size - MFC Vs2010 Pin
UrbanBlues13-Aug-11 5:03
UrbanBlues13-Aug-11 5:03 
AnswerRe: How to get Actual CFormView Size - MFC Vs2010 Pin
Richard MacCutchan13-Aug-11 6:37
mveRichard MacCutchan13-Aug-11 6:37 
GeneralRe: How to get Actual CFormView Size - MFC Vs2010 Pin
UrbanBlues13-Aug-11 22:19
UrbanBlues13-Aug-11 22:19 
GeneralRe: How to get Actual CFormView Size - MFC Vs2010 Pin
Richard MacCutchan13-Aug-11 22:32
mveRichard MacCutchan13-Aug-11 22:32 
AnswerRe: How to get Actual CFormView Size - MFC Vs2010 Pin
«_Superman_»13-Aug-11 8:48
professional«_Superman_»13-Aug-11 8:48 
GeneralRe: How to get Actual CFormView Size - MFC Vs2010 Pin
UrbanBlues13-Aug-11 22:20
UrbanBlues13-Aug-11 22:20 
QuestionHeap corruption - maybe you can see more than me [modified] Pin
blackshadow313-Aug-11 3:19
blackshadow313-Aug-11 3:19 
Hey,

I'm working on an larger project for a while and i've almost tested my application in debug mode, where it works really fine. But later on i've tested it with some brute force methods in release mode, and i've noticed that it almost crashes after some 100 test runs due a heap corruption error. As you may know heap corruption errors are really hard to find, but i've figured out that this error occurs even if I comment out about 99% of my code. Frown | :(

But by checking the remaining lines I find absolutely nothing ... for some moments i thought it could be caused by the reference counter of _variant_t and bstr_t but maybe thats not the error: So at first i will provide the lines to you which already cause the heap corruption:

A simple call in my main:
DDDElements::DDDDocument* pDocument = new DDDElements::DDDDocument();


Which calls this. (CLRObject(...) just sets some local variables.)
DDDDocument::DDDDocument(void) : CLRObject(NULL, NULL, NULL)
{
	clr = new CLR();
	clra = new CLRAssembly(clr, L"DDD");
 	vtCLRObject = clra->CreateInstance(THISDOTNETTYPE());
}


The CLR instanciation looks like this where all used methods are imported:
CLR::CLR(void)
{
	this->pszVersion = L"v4.0.30319";
	this->init();
}

void CLR::init() {
	HRESULT hr;

	BOOL bLoadable = false;
	IUnknownPtr spAppDomainThunk = NULL;
	
    hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID *)&lpMetaHost);
	if(FAILED(hr)) throw "CLR instance could not be created!";

    hr = lpMetaHost->GetRuntime(pszVersion, IID_ICLRRuntimeInfo, (LPVOID *)&lpRuntimeInfo);    
	if(FAILED(hr)) throw "Failed getting the reference to CLR interface!";

	hr = lpRuntimeInfo->IsLoadable(&bLoadable);
	if(FAILED(hr)) throw "Failed loading the CLR!";
	if(!bLoadable) throw "CLR is not loadable!";

	hr = lpRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_PPV_ARGS(&lpRuntimeHost));
	if(FAILED(hr)) throw "Failed loading the CLR!";

    hr = lpRuntimeHost->Start();
	if(FAILED(hr)) throw "Failed starting the CLR!";	

	hr = lpRuntimeHost->GetDefaultDomain(&spAppDomainThunk);
	if (FAILED(hr)) throw "Failed to get CLR default appdomain!";

	hr = spAppDomainThunk->QueryInterface(IID_PPV_ARGS(&spAppDomain));
	if (FAILED(hr)) throw "Failed to get CLR default appdomain!";
}


The Assembly is loaded by this few lines:
CLRAssembly::CLRAssembly(CLR* clr, PCWSTR pszAssemblyName)
{
	HRESULT hr;
	bstr_t bstrAssemblyName(pszAssemblyName);	

	this->clr = clr;
	hr = clr->GetAppDomain()->Load_2(bstrAssemblyName, &spAssembly);
	if(FAILED(hr)) throw "Loading of assembly failed.";
}


Then we first run THISDOTNETTYPE to pass its returnvalue to the create instance method:
C++
_TypePtr DDDDocument::THISDOTNETTYPE() 
{
	return clra->BuildType(L"DDD.DDDDocument");
}

C++
_TypePtr CLRAssembly::BuildType(PCWSTR pszClassName) 
{
	HRESULT hr = NULL;
	bstr_t bstrClassName(pszClassName);	
	_TypePtr spType = NULL;

	hr = spAssembly->GetType_2(bstrClassName, &spType);
	if(FAILED(hr)) throw "Failed to get typereferece.";

	return spType;
}


Until now just some small easy lines. Finally we run create Instance which looks like this:
_variant_t CLRAssembly::CreateInstance(_TypePtr spType) 
{
	SAFEARRAY* psaMethodArgs = NULL;
	psaMethodArgs = SafeArrayCreateVector(VT_VARIANT, 0, 0);
	return this->CreateInstance(spType, psaMethodArgs);
}

_variant_t CLRAssembly::CreateInstance(_TypePtr spType, SAFEARRAY* psaMethodArgs) 
{
	HRESULT hr;
	_variant_t vtResult= NULL;
	_variant_t vtEmpty;

	if(psaMethodArgs == NULL) psaMethodArgs = SafeArrayCreateVector(VT_VARIANT, 0, 0);

    hr = spType->InvokeMember_3(NULL, static_cast<BindingFlags>(
        BindingFlags_CreateInstance | BindingFlags_Instance | BindingFlags_Public),
        NULL, vtEmpty, psaMethodArgs, &vtResult);
	if(FAILED(hr)) throw "Failed to create Instance.";

	return vtResult;
}


And done.

This I run some hundred times and in 1 of 200 runs it ends up in a heap corruption error. I've absolutely no idea why. I've guessed a lot of things and googled for them, but none of them helped.

As already told i've especially looked at the reference counter of the COM types like SAFEARRAY, _variant_t and bstr_t. I thought returning them causes the error. But it seems by returning them by value should be the best answer to avoid heap corruptions there. They first make a copy of themselves, after that the free themselves. So this should work. Then i thought passing them by value could cause the error. But there the reference counter adds a reference - so it works. In the and as already told now the third time - i've absolutely no idea what causes the heap corruption. Maybe one of you will find it by first look at it Wink | ;)

*Edit + Edit 2*
At the moment there are 2 questions I am asking myself:
1. what happens with a reference when it is copied by value, and the old variant containing this refernece is freed? As I know the garbage collector of the CLR should be inactive. But is it deleted or not? The problem is that there is no possibility to trace that. Frown | :(
2. Could this error may be caused by using this as a static library compiled in debug mode? Answer due testing: No.

Regards BS

modified on Saturday, August 13, 2011 10:13 AM

QuestionCannot Interact with service using SERVICE_USER_DEFINED_CONTROL [SOLVED] Pin
vishalgpt13-Aug-11 2:16
vishalgpt13-Aug-11 2:16 
AnswerRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
Richard MacCutchan13-Aug-11 2:21
mveRichard MacCutchan13-Aug-11 2:21 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 2:40
vishalgpt13-Aug-11 2:40 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
Richard MacCutchan13-Aug-11 4:36
mveRichard MacCutchan13-Aug-11 4:36 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 4:59
vishalgpt13-Aug-11 4:59 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
Richard MacCutchan13-Aug-11 5:56
mveRichard MacCutchan13-Aug-11 5:56 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 14:30
vishalgpt13-Aug-11 14:30 
AnswerRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 16:09
vishalgpt13-Aug-11 16:09 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL[SOLVED] Pin
vishalgpt13-Aug-11 16:04
vishalgpt13-Aug-11 16:04 
QuestionBitmap on Picture Control Pin
manju 312-Aug-11 21:18
manju 312-Aug-11 21:18 
AnswerRe: Bitmap on Picture Control Pin
Richard MacCutchan13-Aug-11 0:43
mveRichard MacCutchan13-Aug-11 0:43 
QuestionRe-intilize stack pointer Pin
ForNow12-Aug-11 9:41
ForNow12-Aug-11 9:41 
AnswerRe: Re-intilize stack pointer Pin
MicroVirus12-Aug-11 10:42
MicroVirus12-Aug-11 10:42 
AnswerRe: Re-intilize stack pointer Pin
Richard MacCutchan12-Aug-11 23:00
mveRichard MacCutchan12-Aug-11 23:00 
QuestionAbout wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work? Pin
frankgt4012-Aug-11 7:31
frankgt4012-Aug-11 7:31 
AnswerRe: About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work? Pin
Maximilien12-Aug-11 8:01
Maximilien12-Aug-11 8:01 
GeneralRe: About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work? Pin
frankgt4012-Aug-11 16:56
frankgt4012-Aug-11 16:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.