Perl Error Attempt To Free Unreferenced Scalar
Visit our site Share this post Get in touch Twitter Facebook LinkedIn Dribbble The Dreaded "Attempt to free unreferenced scalar" Steffen Müller Fri 21 June 2013 Every single time I see an error message that looks like Attempt to free unreferenced scalar: SV 0xDEADBEEF, my heart sinks. I know that I am in for an extended debugging session. Debugging memory management problems is always a big hassle. That is true in Perl or any other language. valgrind and similar tools can be a godsend, but even those struggle with certain classes of problems. Understanding the Problem The Perl diagnostics list has the following useful bit to say about the warning: Perl went to decrement the reference count of a scalar to see if it would go to 0, and discovered that it had already gone to 0 earlier, and should have been freed, and in fact, probably was freed. This could indicate that SvREFCNT_dec() was called too many times, or that SvREFCNT_inc() was called too few times, or that the SV was mortalized when it shouldn't have been, or that memory has been corrupted. Let's pull that apart as it references a number of implementation details of Perl: The current implementation of Perl5 uses a reference counting based memory management scheme. Every basic value (a scalar, technically a pointer to an SV struct) has a slot that tracks the number of times it is referenced by anything else. If you create a new reference to an SV, you increment this so-called refcount. After giving up your reference to the SV, you need decrement the counter. As soon as this refcount reaches zero, Perl frees the memory associated with that SV and in turn decrements the refcounts on all other SVs that the soon-to-be-ex-SV holds a reference to. SvREFCNT_inc() and SvREFCNT_dec() are the Perl (C) API macros that do just that. If you call SvREFCNT_inc() one time too many or call SvREFCNT_dec() one time too few, then the SV and everything it references will leak because they never get destroyed until the global destruction phase of the perl VM. If you do the opposite (too many SvREFCNT_dec() or too few SvREFCNT_inc() calls) then the refcount on an SV drops to zero prematurely and it is freed even though it is still referenced by data structures. Alas, those are left in blissful unawareness of the pending doom by invalid memory access. The aforementioned warning is generated by Perl when it detects that the refcount on an SV is being decremented and that refcount was already zero. The refcount be
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each http://blog.booking.com/the-dreaded-attempt-to-free-unreferenced-scalar.html other. Join them; it only takes a minute: Sign up What is this error “Attempt to free unreferenced scalar”? [duplicate] up vote 1 down vote favorite This question already has an answer here: How to investigate “ Attempt to free unreferenced scalar” 5 answers I am invoking a perl script from nodejs and getting errors like: Attempt to http://stackoverflow.com/questions/24382541/what-is-this-error-attempt-to-free-unreferenced-scalar free unreferenced scalar: SV 0x15810d8 during global destruction. Attempt to free unreferenced scalar: SV 0x15810d8 during global destruction. Attempt to free unreferenced scalar: SV 0x15cffb8 during global destruction. Attempt to free unreferenced scalar: SV 0x15cffb8 during global destruction. Segmentation fault Can anyone tell me which kind of error is this and how to debug it because it is simply ending the node process without giving any valuable information. node.js perl segmentation-fault share|improve this question asked Jun 24 '14 at 9:07 napster 19918 marked as duplicate by tchristperl Users with the perl badge can single-handedly close perl questions as duplicates and reopen them as needed. Jun 25 '14 at 23:46 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 2 stackoverflow.com/questions/6848420/… –Сухой27 Jun 24 '14 at 9:10 2 It means you've found a bug in an XS module or a Perl itself. –ikegami Jun 24 '14 at 11:37 add a comment| 1 Answer 1 acti
... _B.pm_ ... sub c{...} ... [download] ERROR_ "Scalars leaked: 1" occurs for each ref or a hash (replace $dummy with %dummy in above script) that is http://www.perlmonks.org/bare/?node_id=607577 passed as argument to "B::c". WARNING_ "Attempt to free unreferenced scalar" always happens in above example when we pass a variable (of any type) in one process to a sub in another https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=650675 module when it is run as a thread. In all above cases, arguments have been initialized before creating threads. In "B::c", it also doesn't matter if those arguments appear as lef-side or perl error right-side (the semaphore and queue definitely change their content and I also tried not to use any argument in "B::c" or just print out some scalar or size of some non-scalar.). So, is this a problem of my script or a bug? I did find there is memory leak by checking w/ valgrind, though I haven't check in very detail. Any comments? Thanks! System: perl perl error attempt v5.8.8 built for i686-linux-thread-multi 2.6.15-gentoo linux Comment on "Attempt to free unreferenced scalar" and "Scalars leaked: 1" ?Download Code Replies are listed 'Best First'. Re: "Attempt to free unreferenced scalar" and "Scalars leaked: 1" ? by Joost (Canon) on Mar 31, 2007 at 00:12UTC You should be careful about naming modules B - there's a core module named B and depending on how you set up your include path, that one might be loaded instead of your own. Also, your code is way too small to make any observations about what's going wrong. It looks like a bug in perl - normal code should never free unreferenced scalars or leak - but if your code tries hard enough you might run into problems :-) "What should it profit a man, if he should win a flame war, yet lose his cool?" [reply] Re^2: "Attempt to free unreferenced scalar" and "Scalars leaked: 1" ? by licht (Novice) on Mar 31, 2007 at 23:38UTC Names "A" and "B" are used for simplicity. I didn't use them in real codes. The following gives short enough but clear code samples that result in 1
is src:libapache2-mod-perl2. Reported by: "Dmitry E. Oboukhov"