Re: clear a array to avoid memory leak

[email protected] (Mark de Vries)
Newsgroups perl.xs
Message-ID <[email protected]>
Walt Chen wrote:
> Hi
> 
> I am new to XS but have coded for perl and C for a few year. Maybe
> this question sounds stupid but I can not figure it out on perlgut and
> perlxs at the moment.

Fairly new to XS myself...

> I have an array which contains several reference of new created
> hashes. I know that if I use method newRV_noinc for av_push in
> construction stage, the reference to each hash is 1.
> 
> After the code does some stuff, I need to clear the array as I want to
> refill it. I have made my code in a concise example as following. Now
> I wonder whether av_clear will decrement the reference number of hash1
> and hash2 to 0. If it does, then hash would be destroyed
> automatically, else I have to destroy it explicitly to avoid memory
> leak. Could anyone point it out for me?

Can't find a definitive statement in the usual docs either. But an
on-line copy of Advanced Perl Programming google found for me says:
"[av_clear] Decrements the reference counts of its constituent scalars
and replaces those positions with undef. It leaves the array intact."

So it seems it will indeed delete/free the hashes.

Cheers,
Mark.

> 
> ==========================================================================================
> int size1 = 20;
> int size2 = 21;
> char name1[] = "foo1";
> char name2[] = "foo2";
> 
> HV *hash1 = newHV();
> hv_store(hash1, "name",  strlen("name"),  newSVpv(name1,0),  0);
> hv_store(hash1, "size",  strlen("size"),  newSViv(size1),  0);
> av_push(array, newRV_noinc((SV*)hash1));  //reference to hash1 is 1
> 
> HV *hash2 = newHV();
> hv_store(hash2, "name",  strlen("name"),  newSVpv(name2,0),  0);
> hv_store(hash2, "size",  strlen("size"),  newSViv(size2),  0);
> av_push(array, newRV_noinc((SV*)hash2));  //reference to hash2 is 1
> 
> .......
> av_clear(array); //whether reference number of hash1 and hash2
> decremented here??
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.