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

Cannot create automation object - lotusscript
DllImport is not found C++
Mangled member function name different during dllimport
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

Categories

HOME
api
design-patterns
delphi-7
ng-idle
saml-2.0
sh
apiblueprint
opendj
seo
big-o
limit
jquery-ui-sortable
categorical-data
alamofire
emoji
stellar.js
dynatrace
esoteric-languages
dimensional-modeling
easyphp
oledb
topology
symbols
user-experience
mount
interbase
appirater
android-maps
8051
datastax-enterprise-graph
tizen-tv
carrierwave
winpe
livelink
scatter3d
dumpbin
playstation-portable
fstream
dsx-desktop
fossil
yowsup
imx6
p4merge
hackintosh
tasm
levenshtein-distance
skylink
wmp
lpsolve
placeholder
puredata
magento-2.0.7
user-defined-fields
reportservice2010
oozie-coordinator
objectscript
branch-prediction
visual-studio-2008-sp1
meteor-packages
pacemaker
iotivity
adafruit
signalr.client
createprocess
ellucian-scribe
database-relations
piracy-prevention
directdraw
assertion
dynatree
google-refine
teamcity-9.1
matlab-deployment
mraid
beatsmusic
addressing-mode
imagefilter
cbind
encryption-asymmetric
insight.database
grails-domain-class
matrix-inverse
wxformbuilder
http-patch
c++builder-xe5
cffile
nevron
actiondispatch
stacky
qi4j
httponly
azman
predicatebuilder
isa-swizzling
box2d-iphone
expression-blend-4
saxparseexception
webdev.webserver
drawtobitmap
cuteeditor
nhprof
zend-rest
application-planning
change-management

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