LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2005, 06:03 PM   #1
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
How do I pass a C++ object reference to GTK+ callbacks???


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 =>
--------------
Code:
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 =>
-------------------
Code:
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 =>
------------------
Code:
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!
 
Old 12-14-2005, 03:32 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Problem fixed: my callback had the wrong signature. (Duh! I forgot the "GtkWidget *w" argument!);

I also changed all my (deprecated!) "gtk_signal_connect()" calls to g_signal_connect()", and all my "gtk_signal_connect_object()" to g_signal_connect_swapped()".

It works fine now.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gtk problem with callbacks mvan83 Programming 3 12-13-2005 06:07 PM
Object Reference problem in Java servlets Hockeyfan Programming 1 06-16-2005 01:36 AM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
function, pass by reference question true_atlantis Programming 5 04-02-2004 11:59 AM
returning a reference to Object C++ chens_83 Programming 3 10-10-2003 09:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration