I am writing an application in C# which uses a C++/CLI Wrapper .dll which again uses another C++/CLI Wrapper for native C code.
Further Explanation:
My C# Application (to which I refer as Reporter) is nothing more than a windows form which calls the first C++/CLI Wrapper (to which I refer to as Control) which contains a UserControl. This UserControl is a GUI in order to call the last .dll (refered to as Generator). I do this because I want to use my Control in other projects and I do not want to hassle with the marshalling of my types like char *
.
So here's my problem: For some times I can call my Generator-Function just as planned. But after some calls, I get an AccessViolationException.
My Generator contains loads of C-functions and also global C variables. Everything is marked properly as extern "C"
. As I determined, the Violation occurs when I try to a global variable.
I was trying to put all the global variables in my wrapper-class in Generator but I failed, because I could not convert all the C-types into a managed-type.
After I call the functions I was free(x)
ing the space of my variables. Before commented that out, I wasn't able to call the function more than twice. Now (after commenting out) I am able to call the function in Generator 4 times. Always.
How can I work around this? Is there a way to give my function like "administrative rights" in order to allow them to do what they want with the global variables?
Thanks to all in advance, I am stuck with this for almost a month now and did much research on how to write wrapper-classes.
Leon
EDIT:
This is how I declare the concerning global variable:
"globals.h"
extern "C"{
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h> // fuer va_start, va_end
#include <string.h>
#include <malloc.h>
#include <windows.h>
//#include <omp.h>
// Used to prevent redefinition, _HAUPT_ is only defined in .dll-Header
#ifdef _HAUPT_
/*#define _HAUPT_*/
#define TYPE
int extreme = 0;
#else
#define TYPE extern
TYPE int extreme;
#endif
}
However, while writing my edit here I found out the problem was on my side. I mixed up a self-written LinkedList
, error occured when I was trying to declare an unallocated pointer with a value.