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

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
Declare and <DllImport> in VB.NET have different results

Categories

HOME
design-patterns
nunit
formal-languages
apache-spark-mllib
solaris
cakephp-3.4
character
discrete-mathematics
categorical-data
endeca
startup
mongodb-csharp
symbol
gruntfile
azure-powershell
entitymanager
asp.net-mvc-viewmodel
imessage
jmp
sar
lilypond
hta
jboss6.x
maze
fat
spotipy
openmdao
constexpr
gitlab-api
mockjax
mailkit
vue-chartjs
reactjs.net
lubridate
searchkit
sim-toolkit
ideascript
tasm
plaintext
http-request
drupal-theming
magento-2.0.7
trojan
carrot2
reachability
donations
gets
mapxtreme
pacemaker
kiosk
uiactionsheet
aspen
computer-architecture
xvim
cgi-bin
iced-coffeescript
mirror
dot.js
angular-gettext
boost-program-options
encryption-asymmetric
insight.database
alternate
terminate
extjs2
ie8-compatibility-mode
printdialog
pagedown
intersect
monotouch.dialog
web-farm
httponly
convention
creole
massive
appledoc
metaweblog
easyb
pos-for-.net
request-headers
duplex
web-application-project
escrow
soappy
wspbuilder

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