EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Development::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=590)
-   -   perlxs and hash refs (https://www.eqemulator.org/forums/showthread.php?t=36183)

c0ncrete 12-31-2012 03:30 AM

perlxs and hash refs
 
this seems to work (i get a hashref with the correct data), but since i'm having a hard time making sense of the perlxs documentation and there doesn't seem to be any code in the server source that returns a reference to a hash in this manner (using eval doesn't count!), i was wondering if i've made any grievous errors here that would cause memory leaks or any other undesirable issues.

Code:

XS(XS_Mob_Loc); /* prototype to pass -Wmissing-prototypes */
XS(XS_Mob_Loc)
{
    dXSARGS;
    if (items != 1)
        Perl_croak(aTHX_ "Usage: Mob::Loc(THIS)");
    {
        Mob * THIS;
        dXSTARG;

        if (sv_derived_from(ST(0), "Mob")) {
            IV tmp = SvIV((SV*)SvRV(ST(0)));
            THIS = INT2PTR(Mob *,tmp);
        }
        else
            Perl_croak(aTHX_ "THIS is not of type Mob");
        if(THIS == NULL)
            Perl_croak(aTHX_ "THIS is NULL, avoiding crash.");

        HV * loc_hash = (HV *)sv_2mortal((SV *)newHV());

        hv_store(loc_hash, "x", 1, newSVnv(THIS->GetX()), 0);
        hv_store(loc_hash, "y", 1, newSVnv(THIS->GetY()), 0);
        hv_store(loc_hash, "z", 1, newSVnv(THIS->GetZ()), 0);
        hv_store(loc_hash, "h", 1, newSVnv(THIS->GetHeading()), 0);

        ST(0) = newRV_noinc((SV *)loc_hash);
    }
    XSRETURN(1);
}


KLS 12-31-2012 03:34 PM

Seems like you handled mortality which is the most obvious problem that can arise with regards to memory.


All times are GMT -4. The time now is 07:34 PM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.