I have some code that looks up an entry in a hash, and adds a nested
subhash if the entry doesn't exist.  The code looks like this:

dperl=/home/rootal/perl/debug/bin/perl

entry = hv_fetch(devicehv, node_name, strlen(node_name), TRUE);
if (! SvOK(*entry))
   {
   subhv = newHV();
printf("SV 1 refcnt = %d\n", SvREFCNT(subhv));
   sv = (SV*)newRV_noinc((SV*)subhv);
printf("SV 2 refcnt = %d\n", SvREFCNT(subhv));
   sv_setsv(*entry, sv);
printf("SV 3 refcnt = %d\n", SvREFCNT(subhv));
   SvREADONLY_on(*entry);
printf("SV 4 refcnt = %d\n", SvREFCNT(subhv));
   }

This prints:

SV 1 refcnt = 1
SV 2 refcnt = 1
SV 3 refcnt = 2
SV 4 refcnt = 2

i.e. the call to sv_setsv increments not the reference count of the RV,
but of the underlying HV.  I assume this is because the *entry is
converted into a new reference to the underlying HV.  To prevent a leak
I need to SvREFCNT_dec(subhv) after the call to sv_setsv - right?  What
happens to the RV pointed to by sv?  Is it a leak as well?  Do I need to
mortalize it so that sv_setsv() will destroy it?

Also, I'm trying to make sure there are no refcount leaks in my code,
which does deep juju with hashes, self ties, magic and chicken
entrails.  I know that the hash tree I build is a binary one, so all
refcounts should be 1.  However, I've noticed that the refcounts inside
a magic substructure aren't 1, and I can't see any discenable pattern: 
for example:

Elt "lread" HASH = 0x0
  SV = PVMG(0x7e8e0) at 0xbf8dc
    REFCNT = 1
    FLAGS = (TEMP,GMG,SMG,RMG)
    IV = 0
    NV = 0
    PV = 0
    MAGIC = 0x128b68
      MG_VIRTUAL = &vtbl_packelem
      MG_TYPE = 'p'
      MG_FLAGS = 0x02
        REFCOUNTED
      MG_OBJ = 0xa9e00
        SV = RV(0xa1344) at 0xa9e00
          REFCNT = 35
          FLAGS = (ROK)
          RV = 0xa9df4
            SV = PVHV(0xa95f0) at 0xa9df4
              REFCNT = 1
              FLAGS = (OBJECT,RMG,SHAREKEYS)
              NV = 0
              MAGIC = 0xa7da8
                MG_VIRTUAL = 0
                MG_TYPE = '~'
                MG_FLAGS = 0x02
                  REFCOUNTED
                MG_OBJ = 0xa9e24
                  SV = PV(0xa33b8) at 0xa9e24
                    REFCNT = 1
                    FLAGS = (POK,pPOK)
                    PV = 0xa7260
"\1\1W@\0\3a\37777777750\0\t\37777777605\30"\0
                    CUR = 12
                    LEN = 13
              STASH = 0x9a028     "Solaris::Kstat::Stat"
              ARRAY = 0x35010  (0:63, 1:43, 2:19, 3:3)
              hash quality = 105.0%
              KEYS = 90
              FILL = 65
              MAX = 127
              RITER = 40
              EITER = 0xb0214
      MG_LEN = -2
      MG_PTR = 0xbf8d0 => HEf_SVKEY
        SV = PV(0xb44ec) at 0xbf8d0
          REFCNT = 3
          FLAGS = (POK,pPOK)
          PV = 0xaa1e8 "lread"\0
          CUR = 5
          LEN = 6

I'm puzzled as to what REFCNT refers to what within the magic structure,
and why they have such non-obvious values - could some kind soul let me
into the secret?

Thanks,

Alan Burlison

Reply via email to