Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Perl Tk Error

by drip (Beadle)
on Sep 13, 2008 at 06:24 UTC ( [id://711060]=perlquestion: print w/replies, xml ) Need Help??

drip has asked for the wisdom of the Perl Monks concerning the following question:

Hela Monks,
i am new to perl tk..and i hope you could help me out with this error message:
Attempt to free non-existent shared string '_TK_RESULT_', Perl interpreter: 0x8885bb0 at /usr/lib/perl5/Tk.pm line 247.

i would appreciate it if someone could tell me what is causing this error message..

---------------------------------------------------------------------------------------------------------------------
i think this is the part of the code where it causes the error or warning message.
sub Rundibbler{ threads->create(sub{my $qxdib=qx/dibbler-client run > dibbler. +out/;})->detach(); threads->create(sub{ my $buff; my $i=1; while(1){ open IN, "dibbler.out" || die "Could not open file:$!"; while(read(IN,$buff,1)){ $reply->insert('end',$buff); } close IN; open OUT, ">dibbler.out" || die "Could not open file:$!"; print OUT " "; close OUT; sleep $i; $i++; }})->detach(); }


Thanks,
drip

Replies are listed 'Best First'.
Re: Perl Tk Error
by zentara (Archbishop) on Sep 13, 2008 at 14:43 UTC
    Tk, is not threadsafe, but you can still use threads with it. Your problem comes from how you are calling the sub Rundibbler. If you call it from a Tk callback, like a Button, timer, etc, it will cause that error. The reason is that when threads get created they get a copy of the parent, at the time of creation. If you are calling it from a Tk callback, or after any Tk statements are in your script, some of the Tk code gets into the thread. Since Tk is NOT threadsafe, everything gets confused, as the Tk code in the parent and thread interfere with each other.

    You can use the Super Search here and search for "Tk threads" and find the same advice given.

    Basically you need to create your threads BEFORE any Tk code is invoked. That usually means you create the threads and have them in a wait loop for a command(thru shared variables) to start running.Also you cannot try to access a Tk widget from a thread( as in your $reply->insert()), you must juggle shared variables in the main thread, and let the main thread adjust the widgets.

    You can try , as a test, to just print to stdout, instead of your thread's $reply->insert(); and see how long it will run. It may well run for awhile, then unexpectedly crash, due to the first reason I mentioned above. BUT if it runs fine, you can share filehandles between threads.

    #in main Tk thread my $fileno = $shash{'fileno'}; #from a shared variable set in thread print "fileno_m $fileno\n"; open (FH, "<&=$fileno") or warn "$!\n"; # filevent works but may not work on win32 $mw->fileevent(\*FH, 'readable', [\&fill_text_widget,$text]); #in thread code block my $pid = open(FH, "top -b |" ) or warn "$!\n"; my $fileno = fileno(FH); $shash{'fileno'} = $fileno;

    To expand your horizons a bit, Perl/Gtk2 has some thread-safety built in, and is easier to use with threads; but it is not perfect, and you can get similar errors with Gtk2.


    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://711060]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found