Re: code crash after 25-30 min only!



On Tue, Apr 16, 2013 at 8:58 PM, Eric Wajnberg <eric wajnberg sophia inra fr
wrote:

Hi there,

I just remain unable to debug this code. Actually, it runs very well but
crashes after 30-35 minutes only.

This is a "stopwatch-type" app. However, I actually need to refresh things
on a continuous basis. Hence, a g_timeout_add_seconds() is **not** what I
need. The idea of a stopwatch is just a way for me to learn and practice.


Jaime your franglais ;-)

I can't tell exactly where or how your app crashes, but a few remarks:

  o If you're studying, it would be better to use GTK+3, as GTK+2 is rather
old now

  o It's unclear to call your timer a widget... calling g_timer_elapsed
(widget2, NULL) looks
     like a crash, but it's not, because widget2 is not a widget, but a
timer (it's possible that
     cleaning up your code to use appropriately named variables will show
you where things
     went wrong).

  o It looks as though you've given a wild pointer to g_signal_connect()
the way you've prototyped
     the OnExpose callback inside main... better to put the prototypes
before main at the global scope.

  o Don't fight the event loop by trying to implement it, I'd argue that
you rather **do** want g_timeout_add()
     to asynchronously refresh the GUI widgets every couple milliseconds.

     Even using a g_idle_add() would ensure that you recalculate your time
every chance that you get.

     Bonus points for getting your rounding correctly and avoiding to call
gtk_widget_queue_redraw()
     when the time you want to display is not any different from the last
time you displayed.

Even if your idle callback runs basically all the time, you do not control
the loop.

Cheers and bonne chance,
        -Tristan




The code that I have is more or less this one:

#include <stdlib.h>
#include <gtk/gtk.h>
#include <strings.h>

int flag_depart=1;
char format_sortie[100];
typedef struct
{
    GtkWidget *widget1;
    GTimer *widget2;
} MyStruct; /* a struct to pass several arguments to an callback */

int main(int argc, char **argv)
{
    /* declaration of widgets */
    [...]
    GtkWidget *pLabel; /* a label */
    GTimer *timer; /* a timer */
    gchar* sUtf8;  /* to format a char string */
    [...]
    MyStruct struct_tempo; /* a struct to pass several arguments to an
callback */
    [...]
    gboolean OnExpose(GtkWidget *pWidget, GdkEvent *event, gpointer
pData); /* function callback expose-event */
    [...]
    /* creation of the label */
    pLabel=gtk_label_new(NULL);
    (void)sprintf(format_sortie, "<span font_desc=\"25\"><b>00 : 00 :
00</b></span>");
    sUtf8 = g_locale_to_utf8(format_**sortie, -1, NULL, NULL, NULL);
    gtk_label_set_markup(GTK_**LABEL(pLabel), sUtf8);
    g_free(sUtf8);
    /* we put the label in the struct */
    struct_tempo.widget1=pLabel;

    /* creation of the timer */
    timer=g_timer_new();
    /* we put the timer in the struct */
    struct_tempo.widget2=timer;

    /* Connexion of signals */
    g_signal_connect(G_OBJECT(**pWindow), "expose-event",
G_CALLBACK(OnExpose), (gpointer )&struct_tempo);

[...]

    return EXIT_SUCCESS;
}
gboolean OnExpose(GtkWidget *pWidget, GdkEvent *event, gpointer pData)
{
    /* updating the label */
    char tempo[1000],h[3],m[3],s[3];
    int heures=0, minutes=0;
    GTimeSpan secondes=0;
    gchar* sUtf8;
    MyStruct *struct_tempo2;
    struct_tempo2= (MyStruct *)pData;
    float convertir(GTimeSpan *secondes, int *minutes, int *heures);
    if (flag_depart)
    {
secondes=(GTimeSpan)g_timer_**elapsed(struct_tempo2->**widget2, NULL);
        convertir(&secondes, &minutes, &heures);
        if (secondes>9)
            (void)sprintf(s, "%d",secondes);
        else
            (void)sprintf(s, "0%d",secondes);
        if (minutes>9)
            (void)sprintf(m, "%d",minutes);
        else
            (void)sprintf(m, "0%d",minutes);
        if (heures>9)
            (void)sprintf(h, "%d",heures);
        else
            (void)sprintf(h, "0%d",heures);
        (void)sprintf(tempo, "<span font_desc=\"25\"><b>%s : %s :
%s</b></span>",h, m, s);
        sUtf8 = g_locale_to_utf8(tempo, -1, NULL, NULL, NULL);
        gtk_label_set_markup(GTK_**LABEL(struct_tempo2->widget1), tempo);
    }
    g_free(sUtf8);
    return FALSE;
}
float convertir(GTimeSpan *secondes, int *minutes, int * heures)
{
    /* convert seconds into hours, minuts and seconds */
    while (*secondes>59)
        {
            *minutes=*minutes+1;
            *secondes=*secondes-60;
        }
        while (*minutes>59)
        {
            *heures=*heures+1;
            *minutes=*minutes-60;
        }
}

This codes works well but crashes avec about 30-35 minutes. I code in
Windows 7 with CodeBlock. I've tried to use a debugger, but I get a:

Segmentation fault.
In ntdll!**LdrWx86FormatVirtualImage () (C:\Windows\system32\ntdll.**dll)

as soon as the gtk_main() is launched. I have really no idea about how I
can solve this now.

Any help will be welcomed!!

Thanks in advance for this.

Cheers, Eric.


--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**~~~~~~~~~~~~~~~~~~~~~~~~
    Eric Wajnberg
    Associated Professor at the
    University of Montreal (Quebec, Canada)
    I.N.R.A.
    400 Route des Chappes, BP 167,
    06903 Sophia Antipolis Cedex, France
    Tel: (33-0) 4.92.38.64.47
    Fax: (33-0) 4.92.38.65.57
    e-mail: wajnberg sophia inra fr
    Web page: http://www.sophia.inra.fr/**perso/wajnberg/<http://www.sophia.inra.fr/perso/wajnberg/>

    Editor-in-Chief of BioControl, Published by Springer.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**~~~~~~~~~~~~~~~~~~~~~~~~

______________________________**_________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/**mailman/listinfo/gtk-app-**devel-list<https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]