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
sonarqube
algorithm
xml
grok
assembly
checkstyle
jmx
toolbox
fftw
solaris
vscode-extensions
nuget
emscripten
onclick
settings
mapbox
iis-6
data-visualization
jtextpane
joomla3.0
reactive-programming
android-securityexception
line
selenium-ide
plink
video-editing
nim
bourbon
replication
selinux
drive
mount
android-things
quantlib
openmdao
gp
carrierwave
left-join
getpixel
xajax
sql-server-ce
metawidget
google-data-api
unordered-map
laravel-elixir
file-sharing
phpdbg
ntvs
term
yii2-api
java-bytecode-asm
compiler-design
android-testing
d3-force-directed
baidu
preprocessor
jboss-tools
mapinfo
synonym
dimension
avspeechsynthesizer
firebase-security
lean
google-query-language
tracker-enabled-dbcontext
php-socket
nullable
maybe
state-restoration
grgit
terracotta
avisynth
ess
morton-number
google-books
rubaxa-sortable
avahi
phpldapadmin
packet-sniffers
deform
linkedin-jsapi
gii
trdion2011
alternate
vbe
amslidemenu
android-audiorecord
printdialog
diem
robotlegs
idn
qi4j
database-create
window-decoration
koken
isa-swizzling
expression-blend-4
exponentiation
socketserver
qglwidget
updatesourcetrigger
purepdf
firefox-3
business-model
movieplayer
commercial-application

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