Discussion:
clear a array to avoid memory leak
(too old to reply)
Walt Chen
2010-05-18 09:49:19 UTC
Permalink
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.

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?


==========================================================================================
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??
Mark de Vries
2010-05-18 14:45:47 UTC
Permalink
Post by Walt Chen
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...
Post by Walt Chen
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.
Post by Walt Chen
==========================================================================================
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??
Walt Chen
2010-05-18 15:58:40 UTC
Permalink
2010/5/18 Mark de Vries
Post by Mark de Vries
Post by Walt Chen
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
Post by Walt Chen
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
"[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.
Hi Mark

Thank you for the inform. I can google the statement you provided. It is
very helpful for me to understand xs programming.
Post by Mark de Vries
Post by Walt Chen
==========================================================================================
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??
Markdv
2010-05-18 14:42:12 UTC
Permalink
Post by Walt Chen
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...
Post by Walt Chen
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.
Post by Walt Chen
==========================================================================================
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??
Loading...