Delegates for returning strings to C# to solve memory leaking problem

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5104 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2003-09-12 21:12:58 +00:00
commit 542dd1c9d6

View file

@ -5,7 +5,6 @@
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%insert(runtime) %{ %insert(runtime) %{
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -36,7 +35,7 @@ typedef struct {
SWIG_CSharpExceptionCallback_t callback; SWIG_CSharpExceptionCallback_t callback;
} SWIG_CSharpExceptions_t; } SWIG_CSharpExceptions_t;
static SWIG_CSharpExceptions_t csharp_exceptions[] = { static SWIG_CSharpExceptions_t SWIG_csharp_exceptions[] = {
{ SWIG_CSharpException, NULL }, { SWIG_CSharpException, NULL },
{ SWIG_CSharpOutOfMemoryException, NULL }, { SWIG_CSharpOutOfMemoryException, NULL },
{ SWIG_CSharpIndexOutOfRangeException, NULL }, { SWIG_CSharpIndexOutOfRangeException, NULL },
@ -48,9 +47,9 @@ static SWIG_CSharpExceptions_t csharp_exceptions[] = {
void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg); void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg);
#else #else
void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg) { void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg) {
SWIG_CSharpExceptionCallback_t callback = csharp_exceptions[SWIG_CSharpException].callback; SWIG_CSharpExceptionCallback_t callback = SWIG_csharp_exceptions[SWIG_CSharpException].callback;
if (code >=0 && code < sizeof(csharp_exceptions)/sizeof(SWIG_CSharpExceptionCodes)) { if (code >=0 && code < sizeof(SWIG_csharp_exceptions)/sizeof(SWIG_CSharpExceptionCodes)) {
callback = csharp_exceptions[code].callback; callback = SWIG_csharp_exceptions[code].callback;
} }
callback(msg); callback(msg);
} }
@ -67,59 +66,100 @@ DllExport void SWIGSTDCALL SWIGRegisterExceptionCallbacks(SWIG_CSharpExceptionCa
SWIG_CSharpExceptionCallback_t divideByZero, SWIG_CSharpExceptionCallback_t divideByZero,
SWIG_CSharpExceptionCallback_t argumentOutOfRange, SWIG_CSharpExceptionCallback_t argumentOutOfRange,
SWIG_CSharpExceptionCallback_t nullReference) { SWIG_CSharpExceptionCallback_t nullReference) {
csharp_exceptions[SWIG_CSharpException].callback = systemException; SWIG_csharp_exceptions[SWIG_CSharpException].callback = systemException;
csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemory; SWIG_csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemory;
csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRange; SWIG_csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRange;
csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZero; SWIG_csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZero;
csharp_exceptions[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRange; SWIG_csharp_exceptions[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRange;
csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReference; SWIG_csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReference;
} }
%} %}
%pragma(csharp) imclasscode=%{ %pragma(csharp) imclasscode=%{
public delegate void SWIGExceptionDelegate(string message); class SWIGExceptionHelper {
static SWIGExceptionDelegate systemDelegate = new SWIGExceptionDelegate(ThrowSystemException); public delegate void SWIGExceptionDelegate(string message);
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);
[DllImport("$module", EntryPoint="SWIGRegisterExceptionCallbacks")] static SWIGExceptionDelegate systemDelegate = new SWIGExceptionDelegate(ThrowSystemException);
public static extern void SWIGRegisterExceptionCallbacks( static SWIGExceptionDelegate outOfMemoryDelegate = new SWIGExceptionDelegate(ThrowOutOfMemoryException);
SWIGExceptionDelegate systemExceptionDelegate, static SWIGExceptionDelegate indexOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowIndexOutOfRangeException);
SWIGExceptionDelegate outOfMemoryDelegate, static SWIGExceptionDelegate divideByZeroDelegate = new SWIGExceptionDelegate(ThrowDivideByZeroException);
SWIGExceptionDelegate indexOutOfRangeDelegate, static SWIGExceptionDelegate argumentOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowArgumentOutOfRangeException);
SWIGExceptionDelegate divideByZeroDelegate, static SWIGExceptionDelegate nullReferenceDelegate = new SWIGExceptionDelegate(ThrowNullReferenceException);
SWIGExceptionDelegate argumentOutOfRangeDelegate,
SWIGExceptionDelegate nullReferenceDelegate);
static void ThrowSystemException(string message) { [DllImport("$module", EntryPoint="SWIGRegisterExceptionCallbacks")]
throw new System.SystemException(message); 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) { static SWIGExceptionHelper exceptionHelper = new SWIGExceptionHelper();
throw new System.OutOfMemoryException(message); %}
}
%insert(runtime) %{
static void ThrowIndexOutOfRangeException(string message) { /* Callback for returning strings to C# without leaking memory */
throw new System.IndexOutOfRangeException(message); typedef char * (SWIGSTDCALL* SWIG_CSharpStringHelperCallback)(const char *);
} static SWIG_CSharpStringHelperCallback SWIG_csharp_string_callback = NULL;
%}
static void ThrowDivideByZeroException(string message) {
throw new System.DivideByZeroException(message); %pragma(csharp) imclasscode=%{
} class SWIGStringHelper {
static void ThrowArgumentOutOfRangeException(string message) { public delegate string SWIGStringDelegate(string message);
throw new System.ArgumentOutOfRangeException(message); static SWIGStringDelegate stringDelegate = new SWIGStringDelegate(CreateString);
}
[DllImport("$module", EntryPoint="SWIGRegisterStringCallback")]
static void ThrowNullReferenceException(string message) { public static extern void SWIGRegisterStringCallback(SWIGStringDelegate stringDelegate);
throw new System.NullReferenceException(message);
} static string CreateString(string cString) {
return cString;
static $modulePINVOKE() { }
SWIGRegisterExceptionCallbacks(systemDelegate, outOfMemoryDelegate, indexOutOfRangeDelegate, divideByZeroDelegate, argumentOutOfRangeDelegate, nullReferenceDelegate);
} 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;
}
%} %}