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:
William S Fulton 2003-09-12 21:12:58 +00:00
commit 3915cf6ecb

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,16 +66,18 @@ 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=%{
class SWIGExceptionHelper {
public delegate void SWIGExceptionDelegate(string message); public delegate void SWIGExceptionDelegate(string message);
static SWIGExceptionDelegate systemDelegate = new SWIGExceptionDelegate(ThrowSystemException); static SWIGExceptionDelegate systemDelegate = new SWIGExceptionDelegate(ThrowSystemException);
@ -119,7 +120,46 @@ DllExport void SWIGSTDCALL SWIGRegisterExceptionCallbacks(SWIG_CSharpExceptionCa
throw new System.NullReferenceException(message); throw new System.NullReferenceException(message);
} }
static $modulePINVOKE() { static SWIGExceptionHelper() {
SWIGRegisterExceptionCallbacks(systemDelegate, outOfMemoryDelegate, indexOutOfRangeDelegate, divideByZeroDelegate, argumentOutOfRangeDelegate, nullReferenceDelegate); 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;
}
%} %}