GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Segmentation Fault through increment of class data

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
shomaster



Joined: 27 Oct 2007
Posts: 1

PostPosted: Sat Oct 27, 2007 2:33 am    Post subject: Segmentation Fault through increment of class data Reply with quote

If I declare an Object:
Code: (Plaintext)
1
Object *renderer = new Object;

and call the increment function in it:
Code: (Plaintext)
1
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: (Plaintext)
1
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(bpress), (gpointer) renderer);

and proceed to use the function:
Code: (Plaintext)
1
2
3
4
5
6
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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP