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
windows
eclipse
oracle12c
scikit-learn
amp-html
case
obfuscation
seo
visual-studio-emulator
azure-container-service
jtextpane
joomla3.0
rcloud
gson
naivebayes
intercom
enterprise-architect
google-container-registry
delphi-xe5
oledb
jquery-ias
named-entity-recognition
visual-studio-extensions
corpus
entity-relationship-model
exchangewebservices
vex
calculation
google-cast
userdefaults
deltaspike
fstream
wacom
papaparse
code-snippets
jett
android-gridview
activexobject
slacktextviewcontroller
apic
reason
wurfl
railstutorial.org
mongoose-schema
apriori
plr
congestion-control
apache-spark-dataset
procfile
linter
manova
magic-draw
reindex
video-conferencing
oracleclient
renaming
gets
docpad
spring-cloud-feign
mongodb-php
jboss-tools
ng-grid
scaffolding
refinerycms
maybe
pax-exam
rpart
evolus-pencil
ember-cli-rails
nutiteq
node-serialport
uvc
osc
actionfilterattribute
ejb-2.x
showdialog
ghcjs
ssis-data-flow
universal-image-loader
insight.database
build-script
mutual-authentication
fpdi
synapse
clickbank
cmmi
abstract-data-type
pagedown
device-width
inkcanvas
kqueue
pinch
winmain
entitydatasource
w3c-geolocation
xui
unattended-processing
codebase
commerceserver2007
facebook-iframe
easyb
dojo-dnd
flvplayback
lemmatization
idatareader

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App