From 609c14457dfeee01d7d55940d40dc0807e5b0041 Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Sat, 18 Feb 2006 01:19:50 +0000 Subject: [PATCH] fix premature object deletion reported by Paul in tcl3d git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@8836 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/test-suite/tcl/union.i | 41 ++++++++++++++++++++ SWIG/Examples/test-suite/tcl/union_runme.tcl | 36 +++++++++++++++++ SWIG/Lib/tcl/tcltypemaps.swg | 24 +++++++++--- 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 SWIG/Examples/test-suite/tcl/union.i create mode 100755 SWIG/Examples/test-suite/tcl/union_runme.tcl diff --git a/SWIG/Examples/test-suite/tcl/union.i b/SWIG/Examples/test-suite/tcl/union.i new file mode 100644 index 000000000..d8ac62bfb --- /dev/null +++ b/SWIG/Examples/test-suite/tcl/union.i @@ -0,0 +1,41 @@ +%module unions + +%inline %{ + +typedef unsigned char Uint8; + +typedef struct SDL_ActiveEvent { + Uint8 type; /* SDL_ACTIVEEVENT */ + Uint8 gain; /* Whether given states were gained or lost (1/0) */ + Uint8 state; /* A mask of the focus states */ +} SDL_ActiveEvent; + +/* Keyboard event structure */ +typedef struct SDL_KeyboardEvent { + Uint8 type; /* SDL_KEYDOWN or SDL_KEYUP */ + int which; /* The keyboard device index */ + int state; /* SDL_PRESSED or SDL_RELEASED */ +} SDL_KeyboardEvent; + +typedef union { + Uint8 type; + SDL_ActiveEvent active; + SDL_KeyboardEvent key; +} SDL_Event; + +int SDL_PollEvent (SDL_Event *ev) { + static int toggle = 0; + if (toggle == 0) { + ev->type = 1; + ev->active.gain = 20; + ev->active.state = 30; + } else { + ev->type = 2; + ev->key.which = 2000; + ev->key.state = 3000; + } + toggle = 1 - toggle; + return 1; +} + +%} diff --git a/SWIG/Examples/test-suite/tcl/union_runme.tcl b/SWIG/Examples/test-suite/tcl/union_runme.tcl new file mode 100755 index 000000000..2c47ecef3 --- /dev/null +++ b/SWIG/Examples/test-suite/tcl/union_runme.tcl @@ -0,0 +1,36 @@ +if [ catch { load ./union[info sharedlibextension] unions} err_msg ] { + puts stderr "Could not load shared object:\n$err_msg" +} + +set event [SDL_Event] + +for { set i 0 } { $i < 2 } { incr i } { +# puts -nonewline "Loop $i: " + set evAvailable [SDL_PollEvent $event] + set evType [$event cget -type] +# puts "evType = $evType" + + if { $evType == 1 } { + set specEvent [$event cget -active] +# puts "specEvent = $specEvent" + set type [$specEvent cget -type] + if { $type != $evType } { + error "Type $type should be $evType" + } + set gain [$specEvent cget -gain] + set state [$specEvent cget -state] +# puts "gain=$gain state=$state" + } + if { $evType == 2 } { + set specEvent [$event cget -key] +# puts "specEvent = $specEvent" + set type [$specEvent cget -type] + if { $type != $evType } { + error "Type $type should be $evType" + } + set which [$specEvent cget -which] + set state [$specEvent cget -state] +# puts "which=$which state=$state" + } +# puts "" +} diff --git a/SWIG/Lib/tcl/tcltypemaps.swg b/SWIG/Lib/tcl/tcltypemaps.swg index a18e6a123..7199e674c 100644 --- a/SWIG/Lib/tcl/tcltypemaps.swg +++ b/SWIG/Lib/tcl/tcltypemaps.swg @@ -64,14 +64,26 @@ * Tcl extra typemaps * ------------------------------------------------------------ */ +#if 1 +// Old 1.3.25 typemaps needed to avoid premature object deletion +%typemap(out,noblock=1) SWIGTYPE *INSTANCE, SWIGTYPE &INSTANCE, SWIGTYPE INSTANCE[] { + Tcl_SetObjResult(interp, SWIG_NewInstanceObj( %as_voidptr($1), $1_descriptor,0)); +} + +%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { + swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor,%as_voidptrptr(&$1)); + Tcl_SetObjResult(interp,SWIG_NewInstanceObj(%as_voidptr($1), ty,0)); +} + +#endif + +%typemap(throws,noblock=1) SWIGTYPE CLASS { + SWIG_set_result(SWIG_NewInstanceObj(%as_voidptr(SWIG_new_copy($1, $1_ltype)), $&1_descriptor, 1)); + SWIG_fail; +} + %typemap(out) SWIGTYPE = SWIGTYPE INSTANCE; %typemap(out) SWIGTYPE * = SWIGTYPE *INSTANCE; %typemap(out) SWIGTYPE & = SWIGTYPE &INSTANCE; %typemap(out) SWIGTYPE [] = SWIGTYPE INSTANCE[]; %typemap(varout) SWIGTYPE = SWIGTYPE INSTANCE; - -%typemap(throws,noblock=1) SWIGTYPE CLASS { - SWIG_set_result(SWIG_NewInstanceObj(SWIG_as_voidptr(SWIG_new_copy($1, $1_ltype)), $&1_descriptor, 1)); - SWIG_fail; -} -