Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Thanks in advance for your help.
I have the following code to convert the unmanaged void* to managed object.
C++
void ListenerCallback(int msg, int p1, int p2, void* p3, void* cookie) 
{
    mController^ mlc;
    // unwrap the native object...
    if(cookie){
    GCHandle h = GCHandle::FromIntPtr(IntPtr(reinterpret_cast<void*>(cookie))); 
	try 
	{ 
	  // and cast the object back out 
	  mlc = safe_cast<mcontroller^>(h.Target); 
	} 
	finally 
	{ 
	   h.Free();
	} 
   }
}


I throws a Fatal error at GCHandle::FromIntPtr.
HTML
The runtime has encountered a fatal error. The address of the error was at 0x71de2e63, on thread 0x1474. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.


Please guide me on solving this.
Thanks.
Posted
Updated 1-Dec-11 15:03pm
v2
Comments
[no name] 1-Dec-11 21:03pm    
EDIT: added emphasis on question

1 solution

The message is completly right: you are doing extremly bad stuff. Sharing objects or even handles is dangerous - even it works sometimes.

You should transfer the raw handle or data AND make a local copy of it and than do your stuff.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900