dllimport


Mixed native and managed code heap corruption


I'm using Visual Studio 2010 and importing a native C++ DLL into C#. When I simply call the functions from the DLL and do nothing else in my C# code everything works as it should, but when I start doing other things in the front end I get the error:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'D:\Jeff\4YP\ImageViewerApp\ImageViewerApp\bin\Debug\ImageViewerApp.vshost.exe'.
Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x6b4f8127, on thread 0x1460. 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.
I believe it is a heap corruption problem. I have tried playing around with the code in the DLL (which is basically exporting a class) and have managed to get the program to run when I remove all lines which assign to (non-pointer) class variables. But pointers and local function variables seem not to lead to the corruption. And rather strangely, there appears to be no corruption for the particular cases of class variables of type int, float, double, but it does occur for my user-defined types and bool (I haven't tried all types).
What could cause this rather confusing scenario?
Here is the code from my C++ DLL:
#pragma once
#include "stdafx.h"
#include "GlobalClassifier.h"
const int frameRate = 33;
const int jointFeaturesCount = 9;
namespace MyKinectDll
{
typedef struct vector
{
public:
float x;
float y;
float z;
} VECTOR;
[event_receiver(native)]
class __declspec(dllexport) MyKinect
{
public:
MyKinect(void)
{
//Fails when these lines are included:
//initialise hand positions
leftHandPos.x = 0;
leftHandPos.y = 0;
leftHandPos.z = 0;
rightHandPos.x = 0;
rightHandPos.y = 0;
rightHandPos.z = 0;
bGotNewGesture = false;
latestGesture = CClassifier::GestureType::Left_Circular;
//It doesn't fail when these lines are included:
//Create sensor
mySensor = new CNuiSensor();
//Create and initialise classifier
myClassifier = new CGlobalClassifier(mySensor, frameRate, jointFeaturesCount);
myClassifier->Initialise("model.dat");
//Initialise sensor
mySensor->Nui_Init();
};
~MyKinect(void)
{
};
private:
CGlobalClassifier *myClassifier;
CNuiSensor *mySensor;
bool bGotNewGesture;
CClassifier::GestureType latestGesture;
VECTOR leftHandPos;
VECTOR rightHandPos;
};
};
And here is the C# import wrapper:
struct UnmanWrap
{
//Import constructor
[DllImport("MyKinectDll.dll", EntryPoint = "??0MyKinect#MyKinectDll##QAE#XZ",
CallingConvention = CallingConvention.ThisCall)]
public static extern void Constructor(ref UnmanWrap t);
}
public class MyKinectWrap
{
private UnmanWrap unman;
public MyKinectWrap()
{
//Add a path to location of the DLL file.
Environment.SetEnvironmentVariable("PATH","D:\\Jeff\\4YP\\MyKinectDll2\\Release");
this.unman = new UnmanWrap();
UnmanWrap.Constructor(ref this.unman);
}
}

Related Links

how to use DllImport in metro-style app
Converting doc file to post script
How can I get a COM component which can be used on Windows phone 8 project with C#
Externs and DLL imports
Mixed native and managed code heap corruption
MarshalDirectiveException
call unmanaged C++ code from C# using pinvoke
Detecting DLL Code Splicing
WOW64 Redirection and LoadLibrary
Using C# dll in Windows phone 7
Python 2.5 Import dll AttributeError
Replacing the Import Table in PE file by standart LoadLibrary
Unresolved external symbol
Declare and <DllImport> in VB.NET have different results
how to import/export dll and use its function
HELP linking library in DLL project

Categories

HOME
microsoftgraph
porting
twitter-bootstrap-3
checkstyle
google-spreadsheet
solver
gradient
rom
mips
onclick
polling
vsm
boto3
nfs
discrete-mathematics
vimdiff
ibeacon-android
ipmitool
nancy
shared-libraries
drive
href
backpack-for-laravel
object-storage
tosca
jquery-callback
draft-js-plugins
xcode-ui-testing
entity-relationship-model
google-earth
nsurl
anki
playstation-portable
twos-complement
apiary.io
dm-script
reactivekit
flatmap
node-mssql
filesystemwatcher
plaintext
smoothstate.js
outputcache
stylecop
uiimage
flask-restful
yii2-api
facebook-pixel
zenhub
flac
objectscript
shinobi
rocks
pure-css
webjars
latin
centrifuge
dup
udpclient
embedding
php-socket
query-by-example
nullable
phpgrid
phonertc
coda
slash
dnssec
google-refine
sip-server
build-script
personalization
grails-domain-class
maven-scm
cfcache
synapse
away3d
alice
argouml
onmousemove
jsr286
jquery-tools
htdocs
catransform3d
cgbitmapcontextcreate
umra
metaweblog
binomial-heap
application-verifier
request-headers
dsl-tools
linfu-dynamicproxy
business-model
wcf-proxy
leader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App