Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to free a HV* created with newHV?

If I write some XS code with a hash that I never expect to return to perl, do I have to free it? If so, how?

The closest I've come up with is hv_undef, but that is only clearing out the contents of the hash, not the hash itself, from what I understand.

HV* hash = newHV();
...
use the hash
...
hv_undef(hash);
like image 736
Eugene Marcotte Avatar asked Dec 24 '12 19:12

Eugene Marcotte


1 Answers

newHV (like newSV, newAV, etc.) sets the reference count of the newly created value to 1. To free it, you just need to decrement it to 0. There's no special function for that for HVs, so just use SvREFCNT_dec:

HV* hash = newHV();
/*
 * use the hash
 */
SvREFCNT_dec((SV *) hash);
like image 103
Ilmari Karonen Avatar answered Nov 05 '22 13:11

Ilmari Karonen