Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: WMI query with Threads

by BrowserUk (Pope)
on Feb 13, 2013 at 23:05 UTC ( #1018654=note: print w/ replies, xml ) Need Help??


in reply to WMI query with Threads

the script is crashing perl with the following messsage.

Hm. Mixing up the trace output from your program with the error message (and posting it without <code></code> blocks so all the newlines get eaten by the html) makes life difficult for those of whom you are asking for help.

The actual message you are receiving is just:

Free to wrong pool 49c7480 not 163ea8, <FILE> line 5 during global des +truction.

And the first thing to say about that is it isn't "a crash". This is an informational message issued only when the program has ended and Perl is freeing up the memory used by the program.

Essentially, the program is finished, done and dusted, -- whether it has worked or not is a different matter -- and Perl has detected an anomaly in its usage of memory whilst cleaning up. It is telling you that you may want to look more closely at it, but it isn't actually crashing; and the program may have completely correctly despite that anomaly.

What the message is telling you is that a piece of memory that was allocated by one thread; is being freed within the context of another thread. Whilst that can cause problems within your program; the fact that it is only being detected "during global destruction" often means that it didn't prevent the program from working or completing.

Now to the possible causes of that anomaly.

The usual cause of this cross-over of memory from one thread to another is accidental closure of a thread subroutine over a variable created (explicitly or implicitly) within the main thread code. And there are two main ways for that to happen.

  1. Non-shared, lexical (my) variables declared in your main thread, and re-used (without re-declaration) within thread subroutines.

    I do not see any of these in your code.

    This can often happen accidentally when thread subroutines are position at the end of the file. You use (and properly declare) a variable within your main program; and then re-use the name within the subroutine, but forget to declare it locally, and thus you get an accidental closure with no warnings.

    My tip for avoiding this is to put your subroutines -- especially those use as threads; but it works for all subroutines -- at the top of your program before your main line code.

    It flies in the face of certain conventions; but it is the conventions that are flawed here.

  2. The use of package-scoped, non-lexical variable -- ie. variables used but not declared with my -- within subroutines used as threads.

    Your thread procedure has two of these:

    Global symbol "$pingit" requires explicit package name at 1018589.pl l +ine 81. Global symbol "$colItems" requires explicit package name at 1018589.pl + line 91.

You also have several other variables that you haven't declared with my:

Global symbol "$linecount" requires explicit package name at 1018589.p +l line 21. Global symbol "$row" requires explicit package name at 1018589.pl line + 23. Global symbol "$line" requires explicit package name at 1018589.pl lin +e 24. Global symbol "$mainparam" requires explicit package name at 1018589.p +l line 28. Global symbol "$thr0" requires explicit package name at 1018589.pl lin +e 29. Global symbol "$thr1" requires explicit package name at 1018589.pl lin +e 33. Global symbol "$thread_count" requires explicit package name at 101858 +9.pl line 53. Global symbol "$pingit" requires explicit package name at 1018589.pl l +ine 67. Global symbol "$colItems" requires explicit package name at 1018589.pl + line 97. 1018589.pl had compilation errors.

These would all be caught for you by use strict;

So, my suggestions to you are:

  1. Add use strict; to the top of your program.

    And then eliminate all those errors by declaring all your variables with my.

  2. Move your GetTimeZone() subroutine to the top of your program.

    Just before print "Starting main program\n"; would be good.

Then try re-running your program. The chances are that those two simple changes will make the problem 'go away', but if they do not, come back and post your modified code and we can try to help you further.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.


Comment on Re: WMI query with Threads
Select or Download Code
Re^2: WMI query with Threads
by vamsinm (Initiate) on Feb 14, 2013 at 00:03 UTC

    Thank you Very much for your reply and time. apologies for not posting it using the correct tags. i have moved the subrotine to the top of the program and declared all variables with my. but still have the same issue.

    i am sorry if i am wrong about crash. the reason, i said perl crashed was because i got a message window

    "Perl command line interpreter has stopped working" close the program or check online for solution and close the program. i copied the porgram details from the window. please see below.
    Problem signature: Problem Event Name: APPCRASH Application Name: perl.exe Application Version: 5.10.0.1005 Application Timestamp: 4a199d7b Fault Module Name: perl510.dll Fault Module Version: 5.10.0.1005 Fault Module Timestamp: 4a199d7a Exception Code: c0000005 Exception Offset: 0009b108 OS Version: 6.1.7601.2.1.0.256.4 Locale ID: 1033 Additional Information 1: 0a9e Additional Information 2: 0a9e372d3b4ad19135b953a78882e789 Additional Information 3: 0a9e Additional Information 4: 0a9e372d3b4ad19135b953a78882e789 Read our privacy statement online: http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409 If the online privacy statement is not available, please read our priv +acy statement offline: C:\WINDOWS\system32\en-US\erofflps.txt

    following is the output

    perl TimeZoneCheck.pl Starting main program 0 server1 1 server2 Host server1 is alive Host server2 is alive server2 India Standard Time (GMT+05:30) Chennai, Kolkata, Mumbai +, New Delhi (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi Free to wrong pool 49cc3c8 not f3ea8, <FILE> line 5 during global dest +ruction.

    modified Code

    use strict; use warnings; use threads; use threads::shared; use Net::Ping; use Switch; use Net::DNS; #use Try::Tiny; #use DBI; use Win32::OLE qw(in with); use Carp; use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; $Win32::OLE::Warn = 3; sub GetTimeZone { my $paramsub = shift; my @subparam = split(/:/, $paramsub); my $subrow = $subparam[0]; my $subhost = $subparam[1]; $subhost =~ s/^\s+//m; $subhost =~ s/\s+$//m; # Create a new ping object my $pingit = Net::Ping->new("icmp"); my $timeout = 10; print "\n$subrow \t $subhost \n"; my $user="domain\\username"; my $pwd="password"; my $locatorObj =Win32::OLE->new("WbemScripting.SWbemLocator") or d +ie "ERROR CREATING OBJ"; $locatorObj->{Security_}->{impersonationlevel} = 3; #return ($subrow, $subhost); # perform the ping if( $pingit->ping($subhost, $timeout) ) { print "Host ".$subhost." is alive\n"; eval { my $objWMIService = $locatorObj->ConnectServer($subhost, + "root\\cimv2", $user, $pwd); if (Win32::OLE->LastError()) { #$sheet->Cells($row,5)->{'Value'} = "$_\n"; print "$_\n"; Win32::OLE->LastError(0); # this clears your error } else { my $colItems = $objWMIService->ExecQuery ("Select * from Win32_Timezone","WQL",wbemFlagRetu +rnImmediately | wbemFlagForwardOnly); if (Win32::GetLastError()) { #$sheet->Cells($row,5)->{'Value'} = "$_\n"; print "$subhost : $_\n"; Win32::OLE->LastError(0); # this clears your error } else{ foreach my $objItem (in $colItems) { print "\n$subhost \t $objItem->{StandardNa +me} \t $objItem->{Caption} \t $objItem->{Description} \n"; #$timezonename = $objItem->{StandardName}; #$timezonecaption =$objItem->{Caption}; #$timezonedescription = $objItem->{Descrip +tion}; ##objOS.CurrentTimeZone/60 & ":" & right(" +00" & objOS.CurrentTimeZone mod 60, 2) } } } }; if ($@) { print "Warning: Error querying object $subhost: $@\n"; #$sheet->Cells($row,5)->{'Value'} = "$@"; #$row++; next; } } else { print "Warning: ".$subhost." appears to be down or icmp packets +are blocked by their server\n"; #$sheet->Cells($row,5)->{'Value'} = "Warning: ".$netbiosname[0 +]." Down"; } $pingit->close(); } print "Starting main program\n"; open FILE, "c:\\serverping.txt" or die "could not open the file"; my @lines = <FILE>; my $linecount = scalar @lines; my $row = 0; my $line =$lines[$row]; $line =~ s/^\s+//m; $line =~ s/\s+$//m; my $mainparam = $row.':'.$lines[$row]; my $thr0 = threads->new(\&GetTimeZone, $mainparam); $row++; $mainparam = $row.':'.$lines[$row]; my $thr1 = threads->new(\&GetTimeZone, $mainparam); $row++; while(1){ if ($thr0->is_joinable()) { $thr0->join; if ($row<$linecount){ $mainparam = $row.':'.$lines[$row]; $thr0 = threads->new(\&GetTimeZone, $mainparam); $row++; } } if ($thr1->is_joinable()) { $thr1->join; if ($row<$linecount){ $mainparam = $row.':'.$lines[$row]; $thr1 = threads->new(\&GetTimeZone, $mainparam); $row++; } } my $thread_count = threads->list(); #print "$thread_count \n"; last if ($row==$linecount && $thread_count == 0); } print "End of main program\n"; close FILE;
      i am sorry if i am wrong about crash. the reason, i said perl crashed was because i got a message window

      That is kind of important information to have omitted from your post.

      The good news is that I have reproduced your problem and have a solution for you.

      The culprit is Win32::OLE. Even this simple threaded code that uses that module in the main thread, fails in exactly the same when when you try to join a thread. Even if that thread makes no use of the module:

      #! perl -slw use strict; use threads; use Win32::OLE qw[ in with ]; my $thread = async { sleep 3; }; $thread->join;

      However, if you require Win32::OLE in the thread(s) where you want to use it, it works fine:

      #! perl -slw use strict; use threads; #use Win32::OLE qw[ in with ]; my $thread = async { require Win32::OLE; Win32::OLE->import( qw[ in with ] ); sleep 3; }; $thread->join;

      So the solution to your immediate problem is to comment out the use Win32::OLE line at the top of your program and replace it with the require and import as shown above in the top of your thread subroutine.

      Try that and see how you get on.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

        Personally, I would try to avoid mixing threads and Win32::OLE, as I fear to omit relevant parts of the dance described in the documentation for Win32::OLE->Initialize(). I would look at the WMI command line tool wmic.exe as an alternative approach to multiprocessing, and launch that from multiple threads. There is some documentation in the MS technet.

        Sorry for the delay in reply. i was stuck with few other tasks to complete and did not get chance to work on this. i just tested this and it worked fine. thank you sooo much for your help!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018654]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-07-29 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (216 votes), past polls