Re: Reference to a C++ object in GTK+ callbacks



AnimationFile must have a public virtual function void start(), and
m_animation must actually point to an Hbafile, so that when you cast the
void* to AnimationFile* it's going to find a vtable slot for start().
If AnimationFile's start() isn't declared virtual, it won't go through
the vtable.

With the debugger, see if 'this' (the first (hidden) parameter to the
start() function) is 0, indicating that the object didn't get passed to
start().

Note that gtk_signal_connect is deprecated and you should be using
g_signal_connect().


  // HTH, Wally

On Tue, 2005-12-13 at 16:00 -0800, Paul Santa Maria wrote:
Hi -

I'm using the GTK+ 2.6.4 that came with Suse 9.3.  I'm coding in C++, but using the "raw" GTK+ C
libraries.

I'm trying to pass a pointer to one of my C++ classes into a callback, so that I can call a method
on that class.  The code looks like this:

1. less hbaview.h =>
   --------------
class HbaviewApp
{
  ...
private:
  AnimationFile *m_animation;
  ...

class Hbafile : public AnimationFile
{
public:
  Hbafile (GtkWidget *da);
  virtual ~Hbafile ();
  virtual void open (const char *fname);
  virtual void close ();
  virtual void start ();
  ...

2. less HbaviewApp.cpp =>
   -------------------
void init ()
  ...
  GtkWidget *play = 
    gtk_image_menu_item_new_from_stock (
      "gtk-media-play", accel_group);
  gtk_widget_show (play);
  gtk_container_add (GTK_CONTAINER (menu3), play);
  gtk_signal_connect (GTK_OBJECT (play), "activate",
                      G_CALLBACK (play_cb), m_animation);
  ...

3. less callbacks.cpp =>
   ------------------
gboolean
play_cb (gpointer data)
{
  // Deference the object pointer to do the actual work
  AnimationFile *ani = (AnimationFile *)data;
  ani->start ();
}

"ani->start()" works fine outside of the callback.  I know my Animation object has been
successfully created and that "m_animation" points to it before I call gtk_signal_connect().

But it crashes with a signal SIGSEGV, Segmentation fault when the play_cb callback tries to call
ani->start().

Any idea what I might be doing wrong?

Or tips for debugging the problem?

Thanx in advance!
   

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]