|
From: Mattia B. <mat...@li...> - 2006-08-03 18:14:23
|
Hi all,
the topic is abviously threads. This program:
==========
#!/usr/bin/perl -w
use strict;
use warnings;
use threads;
use Test::More tests => 4;
use Wx;
my $app = Wx::App->new( sub { 1 } );
my $color = Wx::Colour->new( 0, 10, 20 );
use Devel::Peek;
my $t = threads->create
( sub {
ok( 1, 'In thread' );
} );
ok( 1, 'Before join' );
$t->join;
ok( 1, 'After join' );
END { ok( 1, 'At END' ) };
=========
crashes after test 3 (after join). The reason is that when the thread is
started the perl-space variables are cloned, but the C++-space objects
are not, so bot threads think they hold the last reference to $color,
both try to free the underlying C++ object => segmentation fault.
There are various way to work around this; all involve keeping tracks
of created objects (Wx::Colour instances in this case) and doing something
special at clone time. This solves the problem once you decide what
special action you want to take.
Options:
1 - the new thread is denied access to the parent thread objects
(tried and works; you should not access wxThings from multiple
threads anyway)
2 - clone the C++ instances; for each variable do (more or less)
$var = Wx::Colour->new( $var );
3 - give the new thread access to the original thread's C++ objects
avoiding the double free (i.e. do what happens now, except for the
segfault).
4 - other options?
Any suggestions?
Thanks!
Mattia
The list of classes needing special treatment is:
Wx::DataObject
Wx::DropTarget
Wx::FileSystem
Wx::FSFile
Wx::GridCellAttr
Wx::GridCellAttr
Wx::GridCellEditor
Wx::GridCellRenderer
Wx::HtmlEasyPrinting
Wx::Printer
Wx::XmlResource
Wx::Accelerators
Wx::Bitmap
Wx::Brush
Wx::Cursor
Wx::Font
Wx::Icon
Wx::Image
Wx::ImageList
Wx::Locale
Wx::Palette
Wx::Pen
|