Instead of filling up the mortals stack with as many SVs as there are
elements, just call get-magic before creating the new SV.
(This is not so easy for hashes, as we have keys as well, and
hv_common always calls get-magic on keys.)
See commits
9c744f4f4d7 and
39984de3a8, which fixed leaking bugs, but
inefficiently.
while (relem <= lastrelem) { /* gobble up all the rest */
SV **didstore;
assert(*relem);
- sv = sv_newmortal();
- sv_setsv(sv, *relem);
+ SvGETMAGIC(*relem); /* before newSV, in case it dies */
+ sv = newSV(0);
+ sv_setsv_nomg(sv, *relem);
*(relem++) = sv;
didstore = av_store(ary,i++,sv);
- if (didstore) SvREFCNT_inc_simple_void_NN(sv);
if (magic) {
+ if (!didstore)
+ sv_2mortal(sv);
if (SvSMAGICAL(sv))
mg_set(sv);
}