Delegates for returning strings to C# to solve memory leaking problem
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5104 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
77ffe51f14
commit
3915cf6ecb
1 changed files with 91 additions and 51 deletions
|
|
@ -5,7 +5,6 @@
|
|||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
%insert(runtime) %{
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
|
@ -36,7 +35,7 @@ typedef struct {
|
|||
SWIG_CSharpExceptionCallback_t callback;
|
||||
} SWIG_CSharpExceptions_t;
|
||||
|
||||
static SWIG_CSharpExceptions_t csharp_exceptions[] = {
|
||||
static SWIG_CSharpExceptions_t SWIG_csharp_exceptions[] = {
|
||||
{ SWIG_CSharpException, NULL },
|
||||
{ SWIG_CSharpOutOfMemoryException, NULL },
|
||||
{ SWIG_CSharpIndexOutOfRangeException, NULL },
|
||||
|
|
@ -48,9 +47,9 @@ static SWIG_CSharpExceptions_t csharp_exceptions[] = {
|
|||
void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg);
|
||||
#else
|
||||
void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg) {
|
||||
SWIG_CSharpExceptionCallback_t callback = csharp_exceptions[SWIG_CSharpException].callback;
|
||||
if (code >=0 && code < sizeof(csharp_exceptions)/sizeof(SWIG_CSharpExceptionCodes)) {
|
||||
callback = csharp_exceptions[code].callback;
|
||||
SWIG_CSharpExceptionCallback_t callback = SWIG_csharp_exceptions[SWIG_CSharpException].callback;
|
||||
if (code >=0 && code < sizeof(SWIG_csharp_exceptions)/sizeof(SWIG_CSharpExceptionCodes)) {
|
||||
callback = SWIG_csharp_exceptions[code].callback;
|
||||
}
|
||||
callback(msg);
|
||||
}
|
||||
|
|
@ -67,59 +66,100 @@ DllExport void SWIGSTDCALL SWIGRegisterExceptionCallbacks(SWIG_CSharpExceptionCa
|
|||
SWIG_CSharpExceptionCallback_t divideByZero,
|
||||
SWIG_CSharpExceptionCallback_t argumentOutOfRange,
|
||||
SWIG_CSharpExceptionCallback_t nullReference) {
|
||||
csharp_exceptions[SWIG_CSharpException].callback = systemException;
|
||||
csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemory;
|
||||
csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRange;
|
||||
csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZero;
|
||||
csharp_exceptions[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRange;
|
||||
csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReference;
|
||||
SWIG_csharp_exceptions[SWIG_CSharpException].callback = systemException;
|
||||
SWIG_csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemory;
|
||||
SWIG_csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRange;
|
||||
SWIG_csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZero;
|
||||
SWIG_csharp_exceptions[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRange;
|
||||
SWIG_csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReference;
|
||||
}
|
||||
%}
|
||||
|
||||
%pragma(csharp) imclasscode=%{
|
||||
public delegate void SWIGExceptionDelegate(string message);
|
||||
class SWIGExceptionHelper {
|
||||
|
||||
static SWIGExceptionDelegate systemDelegate = new SWIGExceptionDelegate(ThrowSystemException);
|
||||
static SWIGExceptionDelegate outOfMemoryDelegate = new SWIGExceptionDelegate(ThrowOutOfMemoryException);
|
||||
static SWIGExceptionDelegate indexOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowIndexOutOfRangeException);
|
||||
static SWIGExceptionDelegate divideByZeroDelegate = new SWIGExceptionDelegate(ThrowDivideByZeroException);
|
||||
static SWIGExceptionDelegate argumentOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowArgumentOutOfRangeException);
|
||||
static SWIGExceptionDelegate nullReferenceDelegate = new SWIGExceptionDelegate(ThrowNullReferenceException);
|
||||
public delegate void SWIGExceptionDelegate(string message);
|
||||
|
||||
[DllImport("$module", EntryPoint="SWIGRegisterExceptionCallbacks")]
|
||||
public static extern void SWIGRegisterExceptionCallbacks(
|
||||
SWIGExceptionDelegate systemExceptionDelegate,
|
||||
SWIGExceptionDelegate outOfMemoryDelegate,
|
||||
SWIGExceptionDelegate indexOutOfRangeDelegate,
|
||||
SWIGExceptionDelegate divideByZeroDelegate,
|
||||
SWIGExceptionDelegate argumentOutOfRangeDelegate,
|
||||
SWIGExceptionDelegate nullReferenceDelegate);
|
||||
static SWIGExceptionDelegate systemDelegate = new SWIGExceptionDelegate(ThrowSystemException);
|
||||
static SWIGExceptionDelegate outOfMemoryDelegate = new SWIGExceptionDelegate(ThrowOutOfMemoryException);
|
||||
static SWIGExceptionDelegate indexOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowIndexOutOfRangeException);
|
||||
static SWIGExceptionDelegate divideByZeroDelegate = new SWIGExceptionDelegate(ThrowDivideByZeroException);
|
||||
static SWIGExceptionDelegate argumentOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowArgumentOutOfRangeException);
|
||||
static SWIGExceptionDelegate nullReferenceDelegate = new SWIGExceptionDelegate(ThrowNullReferenceException);
|
||||
|
||||
static void ThrowSystemException(string message) {
|
||||
throw new System.SystemException(message);
|
||||
[DllImport("$module", EntryPoint="SWIGRegisterExceptionCallbacks")]
|
||||
public static extern void SWIGRegisterExceptionCallbacks(
|
||||
SWIGExceptionDelegate systemExceptionDelegate,
|
||||
SWIGExceptionDelegate outOfMemoryDelegate,
|
||||
SWIGExceptionDelegate indexOutOfRangeDelegate,
|
||||
SWIGExceptionDelegate divideByZeroDelegate,
|
||||
SWIGExceptionDelegate argumentOutOfRangeDelegate,
|
||||
SWIGExceptionDelegate nullReferenceDelegate);
|
||||
|
||||
static void ThrowSystemException(string message) {
|
||||
throw new System.SystemException(message);
|
||||
}
|
||||
|
||||
static void ThrowOutOfMemoryException(string message) {
|
||||
throw new System.OutOfMemoryException(message);
|
||||
}
|
||||
|
||||
static void ThrowIndexOutOfRangeException(string message) {
|
||||
throw new System.IndexOutOfRangeException(message);
|
||||
}
|
||||
|
||||
static void ThrowDivideByZeroException(string message) {
|
||||
throw new System.DivideByZeroException(message);
|
||||
}
|
||||
|
||||
static void ThrowArgumentOutOfRangeException(string message) {
|
||||
throw new System.ArgumentOutOfRangeException(message);
|
||||
}
|
||||
|
||||
static void ThrowNullReferenceException(string message) {
|
||||
throw new System.NullReferenceException(message);
|
||||
}
|
||||
|
||||
static SWIGExceptionHelper() {
|
||||
SWIGRegisterExceptionCallbacks(systemDelegate, outOfMemoryDelegate, indexOutOfRangeDelegate, divideByZeroDelegate, argumentOutOfRangeDelegate, nullReferenceDelegate);
|
||||
}
|
||||
}
|
||||
|
||||
static void ThrowOutOfMemoryException(string message) {
|
||||
throw new System.OutOfMemoryException(message);
|
||||
}
|
||||
|
||||
static void ThrowIndexOutOfRangeException(string message) {
|
||||
throw new System.IndexOutOfRangeException(message);
|
||||
}
|
||||
|
||||
static void ThrowDivideByZeroException(string message) {
|
||||
throw new System.DivideByZeroException(message);
|
||||
}
|
||||
|
||||
static void ThrowArgumentOutOfRangeException(string message) {
|
||||
throw new System.ArgumentOutOfRangeException(message);
|
||||
}
|
||||
|
||||
static void ThrowNullReferenceException(string message) {
|
||||
throw new System.NullReferenceException(message);
|
||||
}
|
||||
|
||||
static $modulePINVOKE() {
|
||||
SWIGRegisterExceptionCallbacks(systemDelegate, outOfMemoryDelegate, indexOutOfRangeDelegate, divideByZeroDelegate, argumentOutOfRangeDelegate, nullReferenceDelegate);
|
||||
}
|
||||
static SWIGExceptionHelper exceptionHelper = new SWIGExceptionHelper();
|
||||
%}
|
||||
|
||||
%insert(runtime) %{
|
||||
/* Callback for returning strings to C# without leaking memory */
|
||||
typedef char * (SWIGSTDCALL* SWIG_CSharpStringHelperCallback)(const char *);
|
||||
static SWIG_CSharpStringHelperCallback SWIG_csharp_string_callback = NULL;
|
||||
%}
|
||||
|
||||
%pragma(csharp) imclasscode=%{
|
||||
class SWIGStringHelper {
|
||||
|
||||
public delegate string SWIGStringDelegate(string message);
|
||||
static SWIGStringDelegate stringDelegate = new SWIGStringDelegate(CreateString);
|
||||
|
||||
[DllImport("$module", EntryPoint="SWIGRegisterStringCallback")]
|
||||
public static extern void SWIGRegisterStringCallback(SWIGStringDelegate stringDelegate);
|
||||
|
||||
static string CreateString(string cString) {
|
||||
return cString;
|
||||
}
|
||||
|
||||
static SWIGStringHelper() {
|
||||
SWIGRegisterStringCallback(stringDelegate);
|
||||
}
|
||||
}
|
||||
|
||||
static SWIGStringHelper stringHelper = new SWIGStringHelper();
|
||||
%}
|
||||
|
||||
%{
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
#endif
|
||||
DllExport void SWIGSTDCALL SWIGRegisterStringCallback(SWIG_CSharpStringHelperCallback callback) {
|
||||
SWIG_csharp_string_callback = callback;
|
||||
}
|
||||
%}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue