Article View: perl.qa
Article #1Re: Having memory leak issues with perl-c
From: Mark Murawski
Date: Fri, 12 Aug 2022 23:17
Date: Fri, 12 Aug 2022 23:17
244 lines
15373 bytes
15373 bytes
--------------H2Q5PUxY2WXV6PP15803El3y
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
On 8/12/22 18:26, Mark Murawski wrote:
> On 8/12/22 18:11, Mark Murawski wrote:
>> On 8/4/22 15:10, demerphq wrote:
>>> On Thu, 4 Aug 2022 at 17:04, Mark Murawski
>>> <markm-lists@intellasoft.net> wrote:
>>>
>>> On 8/4/22 02:50, demerphq wrote:
>>>> On Thu, 4 Aug 2022 at 01:58, Mark Murawski
>>>> <markm-lists@intellasoft.net> wrote:
>>>>
>>>> I'm still not getting something... if I want to fix the
>>>> code-as-is and do this:
>>>>
>>>> FNsv = get_sv("main::_FN", GV_ADD);
>>>> if (!FNsv)
>>>> ereport(ERROR,
>>>> (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
>>>> errmsg("couldn't fetch $_FN")));
>>>>
>>>> save_item(FNsv); /* local $_FN */
>>>>
>>>>
>>>> I dont get the sequence here. You take the old value of
>>>> $main::_FN and then you localize it after you fetch it? That
>>>> seems weird.
>>>
>>>
>>> You did not respond to this comment ^^
>>
>>
>> The reason for the save_item*( was because I was modeling this code
>> on another section that uses save_item() to accomplish something
>> similar (to send internal postgres details to some perl). I don't
>> know why the original author uses save_item() for this purpose.
>>
>
> Oh... I know why save_item is used. It's because this code can be
> executed multiple times in the same perl process. So each one needs
> it's own _FN
>
>
After a serious amount of trial and error... I got it to stop crashing
and I fixed the leak
HV *hv; // hash
SV *FNsv; // scalar reference to the hash
SV *SVhv;
ENTER;
SAVETMPS;
FNsv = get_sv("main::_FN", GV_ADD);
save_item(FNsv); /* local $_FN */
hv = newHV(); // create new hash
SVhv = newRV_noinc((SV *) hv);
sv_setsv(FNsv, SVhv);
hv_store_string(hv, "name", cstr2sv(desc->proname));
PUTBACK;
FREETMPS;
LEAVE;
SvREFCNT_dec_current(SVhv);
The fix I found was to put SvREFCNT_dec_current *after*
PUTBACK/FREETMPS/LEAVE
if you do the SvREFCNT_dec_current prior.. then it leaks
If the trailing end of the code looks like this... you get a leak
SvREFCNT_dec_current(SVhv);
PUTBACK;
FREETMPS;
LEAVE;
--------------H2Q5PUxY2WXV6PP15803El3y
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="moz-cite-prefix">On 8/12/22 18:26, Mark Murawski wrote:<br>
</div>
<blockquote type="cite"
cite="mid:1ee614a8-d1af-a3a2-242e-2d3c8304c061@intellasoft.net">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<div class="moz-cite-prefix">On 8/12/22 18:11, Mark Murawski
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:fbad6b32-ae5a-d5c9-3d87-ba5d79f9606e@intellasoft.net">
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<div class="moz-cite-prefix">On 8/4/22 15:10, demerphq wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com">
<meta http-equiv="content-type" content="text/html;
charset=UTF-8">
<div dir="ltr">
<div dir="ltr">On Thu, 4 Aug 2022 at 17:04, Mark Murawski
<<a href="mailto:markm-lists@intellasoft.net"
moz-do-not-send="true" class="moz-txt-link-freetext">markm-lists@intellasoft.net</a>>
wrote:<br>
</div>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div>
<div>On 8/4/22 02:50, demerphq wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">On Thu, 4 Aug 2022 at 01:58, Mark
Murawski <<a
href="mailto:markm-lists@intellasoft.net"
target="_blank" moz-do-not-send="true"
class="moz-txt-link-freetext">markm-lists@intellasoft.net</a>>
wrote:<br>
</div>
<div class="gmail_quote">
<blockquote class="gmail_quote"
style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div>
<div>I'm still not getting something... if I
want to fix the code-as-is and do this:<br>
</div>
<br>
FNsv = get_sv("main::_FN", GV_ADD);<br>
if (!FNsv)<br>
ereport(ERROR,<br>
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),<br>
errmsg("couldn't fetch
$_FN")));<br>
<br>
save_item(FNsv); /* local
$_FN */<br>
</div>
</blockquote>
<div><br>
</div>
<div>I dont get the sequence here. You take the
old value of $main::_FN and then you localize
it after you fetch it? That seems weird.</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
<div><br>
</div>
<div>You did not respond to this comment ^^</div>
</div>
</div>
</blockquote>
<br>
<br>
The reason for the save_item*( was because I was modeling this
code on another section that uses save_item() to accomplish
something similar (to send internal postgres details to some
perl). I don't know why the original author uses save_item()
for this purpose.<br>
<br>
</blockquote>
<br>
Oh... I know why save_item is used. It's because this code can be
executed multiple times in the same perl process. So each one
needs it's own _FN<br>
<br>
<br>
</blockquote>
<br>
<br>
<br>
After a serious amount of trial and error... I got it to stop
crashing and I fixed the leak<br>
<br>
<br>
HV *hv; // hash<br>
SV *FNsv; // scalar reference to the hash<br>
SV *SVhv;<br>
<br>
ENTER;<br>
SAVETMPS;<br>
<br>
FNsv = get_sv("main::_FN", GV_ADD);<br>
save_item(FNsv); /* local $_FN */<br>
<br>
hv = newHV(); // create new hash<br>
SVhv = newRV_noinc((SV *) hv);<br>
<br>
sv_setsv(FNsv, SVhv);<br>
hv_store_string(hv, "name", cstr2sv(desc->proname));<br>
<br>
PUTBACK;<br>
FREETMPS;<br>
LEAVE;<br>
<br>
SvREFCNT_dec_current(SVhv);<br>
<br>
<br>
The fix I found was to put SvREFCNT_dec_current *after*
PUTBACK/FREETMPS/LEAVE<br>
<br>
if you do the SvREFCNT_dec_current prior.. then it leaks<br>
<br>
<br>
If the trailing end of the code looks like this... you get a leak<br>
<br>
<br>
SvREFCNT_dec_current(SVhv);<br>
<br>
PUTBACK;<br>
FREETMPS;<br>
LEAVE;<br>
<br>
<br>
</body>
</html>
--------------H2Q5PUxY2WXV6PP15803El3y--
Technical Details
Message-ID:
<ee42b66b-f559-2a0d-b555-8b0135199453@intellasoft.net>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!apf9.newsdeef.eu!nntp.perl.orgReferences:
<2101aede-608c-9479-6278-cb41dffedc09@intellasoft.net> <CANgJU+XSXiUG0W5wyU-B11k4YD+WootT2LiXwTWYkZTHW7hp+w@mail.gmail.com> <08b0a3a7-41ac-290d-e58a-31faed1bb90a@intellasoft.net> <CANgJU+U0B9-2LN0QS1w7r6CR+VdFYhSMdzidy2cq4aqSzWv6qA@mail.gmail.com> <0cca025e-9365-69bb-ba63-eaa8ccf0cd16@intellasoft.net> <CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com> <fbad6b32-ae5a-d5c9-3d87-ba5d79f9606e@intellasoft.net> <1ee614a8-d1af-a3a2-242e-2d3c8304c061@intellasoft.net>Show Full Headers
Complete Article Headers
Message-ID: <ee42b66b-f559-2a0d-b555-8b0135199453@intellasoft.net> Subject: Re: Having memory leak issues with perl-c Date: Fri, 12 Aug 2022 19:17:14 -0400 From: markm-lists@intellasoft.net (Mark Murawski) References: <2101aede-608c-9479-6278-cb41dffedc09@intellasoft.net> <CANgJU+XSXiUG0W5wyU-B11k4YD+WootT2LiXwTWYkZTHW7hp+w@mail.gmail.com> <08b0a3a7-41ac-290d-e58a-31faed1bb90a@intellasoft.net> <CANgJU+U0B9-2LN0QS1w7r6CR+VdFYhSMdzidy2cq4aqSzWv6qA@mail.gmail.com> <0cca025e-9365-69bb-ba63-eaa8ccf0cd16@intellasoft.net> <CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com> <fbad6b32-ae5a-d5c9-3d87-ba5d79f9606e@intellasoft.net> <1ee614a8-d1af-a3a2-242e-2d3c8304c061@intellasoft.net> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!apf9.newsdeef.eu!nntp.perl.org Newsgroups: perl.qa Return-Path: <markm-lists@intellasoft.net> Mailing-List: contact perl-qa-help@perl.org; run by ezmlm Delivered-To: mailing list perl-qa@perl.org Received: (qmail 12635 invoked from network); 12 Aug 2022 23:17:33 -0000 Received: from xx1.develooper.com (147.75.38.233) by x6.develooper.com with Received: from xx1.develooper.com (xx1.develooper.com [127.0.0.1]) by localhost (Postfix) with SMTP id BE2A37C1C2 for Received: from postfix-inbound-v2-2.inbound.mailchannels.net (inbound-egress-6.mailchannels.net [199.10.31.238]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by xx1.develooper.com (Postfix) with ESMTPS id BB4787C1C1 for Received: from inbound-trex-0 (unknown [127.0.0.6]) by postfix-inbound-v2-2.inbound.mailchannels.net (Postfix) with ESMTP id ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1660346237; a=rsa-sha256; cv=none; b=JS4VMLhYft4suk0N+HeHrTQqULVbdGrEgacK4XF5FKn/dZkEwvADdax37G75QBXIauSY7P 7ZKJ3e6sJKG7+IrAnW0VEtbOjvzo4TA6yAUYJWZRoAuoBC4Lnh0FTg0G7viZb+2SHVnNic u0jy1lIJlFd0bvQRRFHgLvIdPndb6UZAiW3GWLEb4IrBXAV/6RHeAJ3JFRLtYvlAMPOdvr EK1CsIPT9RoAMyqOnHsXCpk+sD7WI6NUJzII/YuC/Y3lCn7WF4eCAGoALoMK4aSBGxSuJY VxyDE27X0pgj1CSEQKS69qZxKswDQyFctVA/LEwRctKKEF7Us/RM9KWJg6agsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1660346237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4dDJsqLc3kVLHG3J1eq7/ZuFQ6sidQbSKpBD3OERDxY=; b=UiSrY9p8BAIViVcO6rzA4dmsCiGCj7nXFp7765hwhmnFVmXqWzNTZyv/pOZWIWkUU44Ou8 FX30T89WXDVFlWhNAGwOS5IiAWHuif7+JIpM+RRWDKm/b1LG9vW5rqMhDqRuyRDTBW4bcu d8nHcqd0NbR7KT8njMEDh6fcwayI1pvqkxT1EznUduh+61rhDGEOXgSjIbGTALp2yuXpNf yzEpXOqn5XTfd15Zg5TchZym9E22JPXYVIlK7qZlrLGeYKqGKOJE6eugCfON9zHUocyvfS F8F+T1HyGgP2KOYmxefmopdwnt2rnAJzkwpJXIx3i8m4sL+h6tPtEkcXsnKZhA== ARC-Authentication-Results: i=1; inbound-rspamd-6ffbf9b6cc-9lvwr; none X-Message-ID: 85nqmFewPsXCYldMuUUg1zrT Received: from mail.bluekaba.com (mail2.intellasoft.net [64.120.46.178]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.127.81.216 Authentication-Results: inbound.mailchannels.net; spf=pass smtp.mailfrom=markm-lists@intellasoft.net; dkim=pass header.d=intellasoft.net; dmarc=pass (policy=none; pct=100; status=pass) Received-SPF: pass (dmarc-service-7b4bff6f5-r7jcn: domain of intellasoft.net designates 64.120.46.178 as permitted sender) client-ip=64.120.46.178; envelope-from=markm-lists@intellasoft.net; helo=mail.bluekaba.com; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=intellasoft.net; s=msb; h=In-Reply-To:References:Cc:To:From:Subject: MIME-Version:Date:Message-ID:Content-Type:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=4dDJsqLc3kVLHG3J1eq7/ZuFQ6sidQbSKpBD3OERDxY=; b=mmFTm2Qjd2NKZ2G+lJNJ5ph4f GdtrgJjyJ7+ZVJqYsZC66AsgPqEER16unKW0iN6L8ONi+T3N7oic1NbBDCxEzBtm8IZOZAvOFxgAH Q44/OTaElybJmcgs0Llf/6DvOsvj6Q6HMSLENcxStaGLPRvh8ezPDLHPDBS42uaufb7SJ0vGjxvJi 5+vLUg/ze/fkqwuzYfW+mMNx1et5YrweG8aE96CmzZpAAGS38jjHuREWfmUW/2zMCbaFDYPMePDMT 0SdRW598nc0pRVtHjRy2DKVEBOzSKISNTmGQ61N7msnr/pCNVer8MjDgNEpMDbKoYjpBwPaMQtXGy o3dKS61lw==; Content-Type: multipart/alternative; boundary="------------H2Q5PUxY2WXV6PP15803El3y" MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Content-Language: en-US To: demerphq <demerphq@gmail.com> Cc: perl-qa@perl.org In-Reply-To: <1ee614a8-d1af-a3a2-242e-2d3c8304c061@intellasoft.net> X-PMX-Version: 5.6.1.2065439, Antispam-Engine: 2.7.2.376379, Antispam-Data: 2022.8.12.230018 X-PMX-Spam: Gauge=IIIIIIII, Probability=8%, Report=' HTML_NO_HTTP 0.1, AUTH_RES_PASS 0, BODYTEXTH_SIZE_10000_LESS 0, BODYTEXTH_SIZE_3000_MORE 0, BODYTEXTP_SIZE_3000_LESS 0, DATE_TZ_NA 0, DKIM_ALIGNS 0, DKIM_SIGNATURE 0, HREF_LABEL_TEXT_NO_URI 0, HREF_LABEL_TEXT_ONLY 0, IN_REP_TO 0, LEGITIMATE_SIGNS 0, MSG_THREAD 0, NO_CTA_URI_FOUND 0, NO_URI_HTTPS 0, REFERENCES 0, SPF_PASS 0, TEXT_DIRECTION 0, TEXT_DIR_LTR_ONLY 0, __ANY_URI 0, __AUTH_RES_DKIM_PASS 0, __AUTH_RES_DMARC_PASS 0, __AUTH_RES_PASS 0, __BAT_BOUNDARY 0, __BODY_TEXT_X4 0, __BOUNCE_CHALLENGE_SUBJ 0, __BOUNCE_NDR_SUBJ_EXEMPT 0, __CT 0, __CTYPE_HAS_BOUNDARY 0, __CTYPE_MULTIPART 0, __CTYPE_MULTIPART_ALT 0, __DKIM_ALIGNS_1 0, __DKIM_ALIGNS_2 0, __DQ_NEG_DOMAIN 0, __DQ_NEG_HEUR 0, __DQ_NEG_IP 0, __FORWARDED_MSG 0, __FRAUD_MONEY_CURRENCY 0, __FRAUD_MONEY_CURRENCY_DOLLAR 0, __FUR_HEADER 0, __HAS_CC_HDR 0, __HAS_FROM 0, __HAS_HTML 0, __HAS_MSGID 0, __HAS_REFERENCES 0, __HEADER_ORDER_FROM 0, __HIGHBITS 0, __HREF_LABEL_TEXT 0, __HTML_AHREF_TAG 0, __HTML_ATTR_DIR 0, __HTML_DIR_LTR 0, __HTML_HREF_TAG_X2 0, __HTML_TAG_DIV 0, __IN_REP_TO 0, __MAIL_CHAIN 0, __MIME_HTML 0, __MIME_TEXT_H 0, __MIME_TEXT_H1 0, __MIME_TEXT_H2 0, __MIME_TEXT_P 0, __MIME_TEXT_P1 0, __MIME_TEXT_P2 0, __MIME_VERSION 0, __MOZILLA_USER_AGENT 0, __MSGID_HEX_844412 0, __PHISH_SPEAR_SUBJ_ALERT 0, __RCPT_DOMAIN_NOT_TO 0, __RCVD_PASS 0, __REFERENCES 0, __SANE_MSGID 0, __SHIPPING_ACTION 0, __SUBJ_ALPHA_NEGATE 0, __SUBJ_REPLY 0, __TAG_EXISTS_HTML 0, __TEXT_DIR_LTR 0, __TO_GMAIL 0, __TO_MALFORMED_2 0, __TO_NAME 0, __URI_MAILTO 0, __URI_NO_WWW 0, __URI_NS , __USER_AGENT 0, __zen.spamhaus.org_ERROR ' Approved: news@nntp.perl.org