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?