Hello,

I have a segmentation fault problem on more than one Linux variant. I
have gathered information about my problem from searching and visiting
from the URL
http://rt.cpan.org/Public/Bug/Display.html?id=16053
and some others that had to do with
"Segmentation fault after destroying non-dependent window"

The comments at those sites make it apparent that I am going about what
I do in the wrong way.

In essense, my old code has a while loop wherein I call a main menu,
amongst other things. From the main menu I get back one of many
possible choices. Each choice may or may not call other subroutines
that may or may not each invoke there own Tk pop ups. Each pop up has
it's own main window, and it's own MainLoop. When the user clicks an
"Okay" button on the popups, the main window is destroyed and I
eventually get back to the original while loop. While this works fine
on Windows, it causes segmentation faults in a number of Linux
variants. It is the equivalent of having the code below:

my $count = 0; my $message = "Get a Segmentation fault within 20 clicks of Okay: "; my $answer = "Okay"; while ($answer eq "okay") { pop_up_a_message($message . $count); $answer = return_exit_if_count_gt_5($count++); }

I tried a simple redesign wherein I first instantiate a MainWindow
($mw), hiding it right away. I changed the routine
pop_up_a_message to send in $mw as well as the $message, and within
pop_up_a_message I have the statement

$we_top = $mw->Toplevel();

I then use $we_top to instantiate the pop up Label (which has the
message) and an "Okay" button. When the user clicks the Okay button
in my pop_up_a_message() routine, $we_top gets destroyed and the
pop_up_a_message() routine returns.

At first I had the "MainLoop;" in the pop_up_a_message() routine. I
forgot that it would not work because even though $we_top was
destroyed when the user clicked the Okay button, the MainWindow was
still alive, and hence would not get past the Mainloop of the
pop_up_a_message() routine. I next moved the MainLoop statement to
after the end of the while loop. I immediately got 5 pop ups, so that
would not work either.

I then moved the "MainLoop;" statement to be the last statement within
the while loop. I got the first pop up, but then no more since $mw is
still alive. It will not get past the MainLoop.

So ... how does one be able to have a program with pop ups at
different times, in different places, given widely varying user
responses. A lot of these popups have Yes/No and other answers that
need to get returned.

Just to be complete, the segmentation fault code that I posted on the
other forums is shown below.

#!/usr/bin/perl -w # File test_pop_up_a_message.pl use Tk; use strict; our $answer = "okay"; ############ sub response { my ($we_top, $button_response) = @_; $we_top->destroy; $answer = $button_response; } ############ sub pop_up_a_message { my ($message) = @_; my $okay_button; my $exit_button; print("Can allways see\n"); my $we_top = new MainWindow; print("Cannot see if Segmentation fault\n"); $we_top->Label ( -text => $message)->pack(); #......... $okay_button = $we_top->Button( -text => 'Okay', -command => [ \&response, $we_top, 'okay' ] )->pack(); #......... $exit_button = $we_top->Button( -text => 'Exit', -command => [ \&response, $we_top, 'exit' ] )->pack(); #......... $we_top->MainLoop; } ############ my $message = ""; my $count = 1; while ($answer eq "okay") { $message = "On at least Slackware and Mandriva, Perl 5.8.7 or 5.8.8, this code +will\n" . " get a Segmentation fault within 20 clicks of Okay \n " . $count++ . "\n"; pop_up_a_message($message); print ("You pressed $answer\n"); }

Any ideas?

Thanks

Edited by planetscape - added readmore tags


In reply to "Segmentation fault after destroying window" by mnooning

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.