GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Sep 18, 2014 1:40 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Segmentation Fault through increment of class data
PostPosted: Sat Oct 27, 2007 2:33 am 
Offline

Joined: Sat Oct 27, 2007 2:13 am
Posts: 1
If I declare an Object:
Code:
Object *renderer = new Object;

and call the increment function in it:
Code:
renderer->incx((double)10);

the variable is incremented by the value specified.
However if I pass the variable as an argument as data in a g_signal_connect:
Code:
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(bpress), (gpointer) renderer);

and proceed to use the function:
Code:
gint bpress(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
   //Object *inc;
   //inc = (Object*)data;
   ((Object*)data)->incx((double)1);
}


when the button is pressed I receive a segmentation fault.
I know the call to the function is working because I checked if the value was getting passed to it with g_print. It only seg faults when the value of xval is changed.

I tried g_print("%d",(int)xval);
If the function is called before it is passed the value is 0, which it should be. If it is called from the button press event the value is way off, suggesting that xval isn't xval anymore, rather it was deleted and it is pointing to a random location.

The class declaration is:
Code:
class Object
{
   public:
      Object();
      int render(cairo_t *cr, GtkWidget *widget);
      void incx(double xInc);
   private:
      double xval;
};

Object::Object()
{
   xval = (double)0;
}

int Object::render(cairo_t *cr, GtkWidget *widget)
{
   cr = gdk_cairo_create(widget->window);
   cairo_set_source_rgb(cr, 0, 0, 0);
   cairo_rectangle(cr, xval,xval,100,100);
   cairo_fill(cr);
}

void Object::incx(double xInc)
{
   xval += xInc;
   g_print("%d",(int)xval);
}


Why is the increment causing the seg fault?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group