Thread: Problem with structures

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12

    Problem with structures

    Hi

    I tried make one of my first program with GTK, but I found problem.
    I wanted to use g_idle_add, and I need also send to my function
    function few informations:

    Code:
    GtkWidget *box_1, *box_2;
    I created structure (Kont) to keep them together.

    Code:
    struct Kont
    {
          GtkWidget * kontrolka_1;
          GtkWidget * kontrolka_2;
    };
    ....
    struct Kont  Pudlo={box_1,box_2};
    Now I send it to the U_function as a &Pudlo
    Code:
    lis_idle = g_idle_add ((GSourceFunc) U_function, &Pudlo);
    The problem is : outside in main function Pudlo.kontrolka_... has something but
    inside U-function I've got nothing. Bud->kontrolka_...=0.


    Code:
    static gboolean U_function (struct Kont  *Bud)
    {
     ....
       GtkWindow * dialog;
       dialog = gtk_message_dialog_new (GTK_WINDOW (window),
       GTK_DIALOG_MODAL,   GTK_MESSAGE_INFO,   GTK_BUTTONS_CLOSE,
      "box_1  = %d \n, box_2  = %d \n" ,
      Bud->kontrolka_1, Bud->kontrolka_2);
    ....
    }
    What is more confusing: on Windows (Dev-C++) all kontrolka=0 but
    when I tried to compile this code on linux it works and they have
    correct values.

    Can anyone tell my why?
    Do I make any mistake with structures?
    Why windows and linux works different?

    Daniel

    (full code I can send on request)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You have to be really careful when passing data to be used in callback functions at a later time. For example, a pointer to a local variable is rarely a good choice (unless it's local to main say).

    Code:
    void foo ( ) {
        // stuff
        struct Kont  Pudlo={box_1,box_2};
        // stuff
        lis_idle = g_idle_add ((GSourceFunc) U_function, &Pudlo);
    
        // and return
        // OOPS, Pudlo is local, and is about to vanish.
        // If U_function tries to use it, that's bad news.
    }
    > when I tried to compile this code on linux it works and they have correct values.
    It's just the way of the universe that sometimes you can screw up big-style and it still appears to do the right thing. Yet at other times even the most minor mistake is punished harshly.

    What you can be sure of is that whenever you see different behaviour on two systems that it's a pretty good sign that you're doing something wrong, even if one of them seems to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    Thanks a lot.
    I did not have idea how exactly works g_idle_add function.


    Daniel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM