There are so many problems with your test code that you are almost certainly not testing the code you think you are. See the comments below:

package test; require Exporter; our @ISA =qw(Exporter); our @EXPORT =qw(queuereader); ## You are exporting "queuereader" our @VERSION =1.0; sub forthread{ ## Not the forthread function that is in the f +ile! my ($arg)=@_; return 0; } #forthread ## You have no module termination value, 1;

All of which leads me to believe that you aren't actually running this module when you run test.pl. Instead you are probably picking up another module called test from somewhere else.

Now to test.pl

use threads; use test; my $argument=1; ## VVVVVVVVVV---- this is not a function refe +rence, its a string! my $thread=threads->create("forthread", ($argument)); $thread->join(); exit(0);

If I attempt to run this pair of files:

## test.pl use threads; use Testit; my $argument=1; my $thread=threads->create( 'forthread', ($argument)); $thread->join(); exit(0); ## Testit.pm package Testit; require Exporter; our @ISA =qw(Exporter); our @EXPORT =qw(forthread); our @VERSION =1.0; sub forthread{ my ($arg)=@_; warn "$arg"; return 0; } 1;

I get the following output:

C:\test>test.pl 1 at Testit.pm line 11. Attempt to free unreferenced scalar: SV 0x194aa50, Perl interpreter: 0 +x18290c4 during global destruction.

Which shows that the function forthread has been exported from the package Testit into main, and has been run as a thread, but when the cleanup of the thread happens, it detects a leaked scalar--the string you passed.

However, if I modify test.pl to pass a coderef rather than a string to the thread creation

use threads; use Testit; my $argument=1; my $thread=threads->create( \&forthread, ($argument)); $thread->join(); exit(0); __END__ C:\test>test.pl 1 at Testit.pm line 11.

I get no error. There are two ways of looking at this. The ability to pass a subroutine name as a string (and possibly a bareword if not using strict?) is a throwback to earlier time (perl 4?), but the code underlying the autoresolution of a string to a coderef is still embedded in the API code. However, newer modules and features may have been written by authors who never used this feature, and so their code does not cater for it.

Specifically, the string containing the function name is duplicated as a string for use on the thread that will be created, but because the author(s) of the threads module where expecting a coderef, they do not deal with this situation. So the other way of looking at it is that this is a bug in the threads module somewhere.

The simplest way of avoiding it is to pass a coderef in the first place.

And when you post demo code, it would be nice if it actually worked to demonstrate the problem described :) test.pm is a bad choice of module/package name.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^3: "Attempt to free unreferenced scalar" and "Scalars leaked: 1" ? by BrowserUk
in thread "Attempt to free unreferenced scalar" and "Scalars leaked: 1" ? by licht

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.