On Monday, September 2, 2002, at 11:35 AM, Sisyphus wrote:
> Only time I used 'malloc()' and 'free()' in an inline function 
> I struck problems if the function was called a number of times. 
> I forget just how big that 'number of times' was - and the 
> amount of memory involved might well have had a bearing. (It 
> worked fine if the function was called only once - which led me 
> to believe that 'free()' was not doing the job, and the memory 
> was being freed only when the app terminated.)
>
> Someone posted the recommendation that 'New()' and 'Safefree()' be used
> instead, and I've been using them with no trouble.

I looked a little closer at the docs, and I think you're right.  
So here's an updated version of the constructor/destructor 
routines:

SV* new(char* class, char* name, char* rank, long serial) {
   Soldier* soldier;
   New(0, soldier, 1, Soldier);

   soldier->name = strdup(name);
   soldier->rank = strdup(rank);
   soldier->serial = serial;

   return sv_setref_pv(newSViv(0), class, (void *)soldier);
}

void DESTROY(SV* obj) {
   Soldier* soldier = (Soldier*)SvIV(SvRV(obj));
   Safefree(soldier->name);
   Safefree(soldier->rank);
   Safefree(soldier);
}


It would also be nice to show a typemap that gets rid of all the 
typecasting, object wrapping, etc. here and just lets you use a 
Soldier* type in prototypes.

  -Ken

Reply via email to