Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

segmentation fault while using thread and gtk2

by Muskovitz (Scribe)
on Jan 07, 2015 at 15:25 UTC ( [id://1112496]=perlquestion: print w/replies, xml ) Need Help??

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

hi monks im having trouble with this problem, this little example code will check the username if it's existing on mysql server.

#!/usr/bin/perl use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use threads; use threads::shared; use DBI; use DBD::mysql; our $type="mysql"; our $database="username"; our $host="http://mysite.com"; our $port="3306"; our $tablename="login"; our $user="username"; our $pwd="************"; our $dsn="dbi:$type:$database:$host:$port"; our $query; our $queryhandle; my $str:shared=0; my $window=Gtk2::Window->new; $window->signal_connect('delete_event',sub{Gtk2->main_quit;}); $window->set_title("Perl Gtk2 Tutorial"); my $vbox=Gtk2::VBox->new; my $label=Gtk2::Label->new("Username: "); my $button=Gtk2::Button->new("Check"); my $entry=Gtk2::Entry->new(); $button->signal_connect(clicked=>sub{ $str=$entry->get_text; my $thr=threads->create(\&continue_checking); }); $vbox->add($label); $vbox->add($entry); $vbox->add($button); $window->add($vbox); $window->show_all; Gtk2->main; sub continue_checking{ our $connect=DBI->connect($dsn,$user,$pwd)or die &mysql_Err; $query="SELECT username FROM login WHERE username=\"$str\""; $queryhandle=$connect->prepare($query); $queryhandle->execute; $queryhandle->bind_columns(undef, \my $user_name); while ($queryhandle->fetch()) { if("$str" eq "$user_name"){ &main; } } } sub main{ my $mw=Gtk2::Window->new; $mw->set_default_size(250, 250); my $label=Gtk2::Label->new("username $str is already exist"); $mw->add($label); $mw->show_all; $mw->show; } ->Error Result<- GLib-GObject-WARNING **: cannot register existing type `GConnectFlags' + at /usr/lib/perl/5.14/DynaLoader.pm line 207. Pango-WARNING **: shaping failure, expect ugly output. shape-engine='B +asicEngineFc', font='DejaVu Sans 9.9990234375', text='' at server.pl +line 40. Segmentation fault

is this like Tk problem that you cant execute a thread inside Gtk2 codes?

Replies are listed 'Best First'.
Re: segmentation fault while using thread and gtk2
by SuicideJunkie (Vicar) on Jan 07, 2015 at 16:05 UTC

    What is it you're trying to do there?

    Bad things tend to happen if you try to mix threads and Tk.

    A much safer way to go about it is to start your threads up front and keep them isolated from each other. Have one thread which does all of the GUI & Tk stuff. Have another thread (or thread pool) for doing background processing. Only the single GUI thread should ever touch the Tk elements, and the processing threads should pass their results to the GUI thread (eg: via shared memory) and let it draw the results.

    Don't spawn new threads from the GUI thread, and definitely don't do it after you have started creating Tk elements unless you like coding in the Twilight Zone.

    PS: The word you're looking for is segmentation, not sigmentation. Searches won't work very well that way.

      do you mean that i must execute a thread before those Gtk2 codes but what i am trying to do is when that button clicked a thread will start and execute!
Re: segmentation fault while using thread and gtk2
by chacham (Prior) on Jan 07, 2015 at 16:21 UTC

    Side comment: The query uses dynamic SQL in that it includes a variable for the username inside the SQL statement. Perhaps you can replace the variable with a placeholder, and pass the variable when executing the statement. With the prepare() there you're already half way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found