Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpalmer committed Mar 6, 2011
1 parent 8f36d4f commit 0175469
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,6 +1,7 @@
Revision history for Perl extension SDL_perl. Revision history for Perl extension SDL_perl.


* *
- SDLx::Controller::Interface fixed memory leak [jtpalmer]
- SDLx::Controller::Interface fixed angular velocity [jtpalmer] - SDLx::Controller::Interface fixed angular velocity [jtpalmer]


* 2.531 Feb 27 2011 * 2.531 Feb 27 2011
Expand Down
70 changes: 41 additions & 29 deletions src/SDLx/Controller/Interface.xs
Expand Up @@ -3,6 +3,7 @@
#include "XSUB.h" #include "XSUB.h"
#include "ppport.h" #include "ppport.h"
#include "defines.h" #include "defines.h"
#include "helper.h"


#ifndef aTHX_ #ifndef aTHX_
#define aTHX_ #define aTHX_
Expand All @@ -13,49 +14,37 @@


AV* acceleration_cb( SDLx_Interface * obj, float t ) AV* acceleration_cb( SDLx_Interface * obj, float t )
{ {

SV* tmpsv; SV* tmpsv;
if( !(SvROK(obj->acceleration) && (tmpsv = obj->acceleration) ) ) if( !(SvROK(obj->acceleration) && (tmpsv = obj->acceleration) ) )
{ {
croak( "Interface doesn't not contain an acceleration callback" ); croak( "Interface doesn't not contain an acceleration callback" );
} }



dSP; dSP;
AV* array = newAV(); AV* array = newAV();
int i;
int count; int count;
SV * stateref = newSV( sizeof(SDLx_State *) ); SDLx_State* copyState = (SDLx_State *)safemalloc( sizeof(SDLx_State) );
void * copyState = safemalloc( sizeof(SDLx_State) ); copy_state( copyState, obj->current );
memcpy( copyState, obj->current, sizeof(SDLx_State) ); copyState->owned = 0;
((SDLx_State *)copyState)->owned = 0; /*conditional free */
ENTER; ENTER;
SAVETMPS; SAVETMPS;
PUSHMARK(SP); PUSHMARK(SP);

XPUSHs( sv_2mortal(newSVnv(t)) );
void** pointers = safemalloc(3 * sizeof(void*)); XPUSHs( sv_2mortal( obj2bag( sizeof(SDLx_State *), (void *)copyState, "SDLx::Controller::State" ) ) );
pointers[0] = (void*)copyState;
pointers[1] = (void*)0;
Uint32 *threadid = (Uint32 *)safemalloc(sizeof(Uint32));
*threadid = SDL_ThreadID();
pointers[2] = (void*)threadid;
SV * state_obj = sv_setref_pv( stateref, "SDLx::Controller::State", (void *)pointers);

XPUSHs( sv_2mortal(newSVnv(t)));
XPUSHs( state_obj );

PUTBACK; PUTBACK;


count = call_sv(obj->acceleration, G_ARRAY); count = call_sv( obj->acceleration, G_ARRAY );


SPAGAIN; SPAGAIN;
/* warn( "state %p, state->x %f", copyState, ((SDLx_State *)copyState)->x ); */ /* warn( "state %p, state->x %f", copyState, ((SDLx_State *)copyState)->x ); */
int i; for( i = 0; i < count; i++ )
for( i =0; i < count ; i++) av_push( array, newSVnv(POPn) );
av_push( array, newSVnv(POPn));


/* warn ("before obj->current->x %f", obj->current->x); */ /* warn ("before obj->current->x %f", obj->current->x); */
copy_state(obj->current, (SDLx_State *)copyState); copy_state( obj->current, copyState );
/* warn ("after obj->current->x %f", obj->current->x); */ /* warn ("after obj->current->x %f", obj->current->x); */
PUTBACK;
FREETMPS; FREETMPS;
LEAVE; LEAVE;


Expand All @@ -68,10 +57,21 @@ void evaluate(SDLx_Interface* obj, SDLx_Derivative* out, SDLx_State* initial, fl
out->dy = initial->v_y; out->dy = initial->v_y;
out->drotation = initial->ang_v; out->drotation = initial->ang_v;
AV* accel = acceleration_cb(obj, t); AV* accel = acceleration_cb(obj, t);
out->dv_x = sv_nv(av_pop(accel));
out->dv_y = sv_nv(av_pop(accel));
out->dang_v = sv_nv(av_pop(accel));


SV* temp;
temp = av_pop(accel);
out->dv_x = sv_nv(temp);
SvREFCNT_dec(temp);

temp = av_pop(accel);
out->dv_y = sv_nv(temp);
SvREFCNT_dec(temp);

temp = av_pop(accel);
out->dang_v = sv_nv(temp);
SvREFCNT_dec(temp);

SvREFCNT_dec((SV*)accel);
} }


void evaluate_dt(SDLx_Interface* obj, SDLx_Derivative* out, SDLx_State* initial, float t, float dt, SDLx_Derivative* d) void evaluate_dt(SDLx_Interface* obj, SDLx_Derivative* out, SDLx_State* initial, float t, float dt, SDLx_Derivative* d)
Expand All @@ -90,9 +90,21 @@ void evaluate_dt(SDLx_Interface* obj, SDLx_Derivative* out, SDLx_State* initial,
out->drotation = state.ang_v; out->drotation = state.ang_v;


AV* accel = acceleration_cb(obj, t+dt); AV* accel = acceleration_cb(obj, t+dt);
out->dv_x = sv_nv(av_pop(accel));
out->dv_y = sv_nv(av_pop(accel)); SV* temp;
out->dang_v = sv_nv(av_pop(accel)); temp = av_pop(accel);
out->dv_x = sv_nv(temp);
SvREFCNT_dec(temp);

temp = av_pop(accel);
out->dv_y = sv_nv(temp);
SvREFCNT_dec(temp);

temp = av_pop(accel);
out->dang_v = sv_nv(temp);
SvREFCNT_dec(temp);

SvREFCNT_dec((SV*)accel);
} }


void integrate( SDLx_Interface* object, float t, float dt) void integrate( SDLx_Interface* object, float t, float dt)
Expand Down
12 changes: 7 additions & 5 deletions src/SDLx/Controller/State.xs
Expand Up @@ -3,6 +3,7 @@
#include "XSUB.h" #include "XSUB.h"
#include "ppport.h" #include "ppport.h"
#include "defines.h" #include "defines.h"
#include "helper.h"


#ifndef aTHX_ #ifndef aTHX_
#define aTHX_ #define aTHX_
Expand Down Expand Up @@ -88,10 +89,11 @@ state_ang_v(state, ...)
RETVAL RETVAL


void void
state_DESTROY( obj ) state_DESTROY( bag )
SDLx_State *obj SV *bag
CODE: CODE:
if (obj->owned == 0) SDLx_State *obj = (SDLx_State *)bag2obj(bag);
safefree(obj); if (obj->owned == 0)
objDESTROY(bag, safefree);




0 comments on commit 0175469

Please sign in to comment.