Re: [gtk-list] Re: Heisenberg was right?



On Wed, 10 Jun 1998, Rouat manu wrote:

> Allan Black wrote:
> 
> > 
> > Of all the types of bug that I've had to track down, the one that goes
> > away when you insert a print statement is the worst of all!
> 
> I know what you mean!
> 
> 
> > Now: the bad news :-(
> > 
> > There is a good (or bad?) chance that the piece of code which contains the
> > bug is nowhere near the code where the program crashes. What can happen is,
> > a corrupt pointer corrupts another pointer, which corrupts another pointer,
> > ...... eventually, you use one of the corrupted pointers and crash.
> 
> I this case I think it's not too bad - I've replaced and simplified
> the code that I suspect creates the trouble, and the bug apparently
> dissapeared - here is the code:
> 
> (from main:)
> 
>   char buffer[32];
>   sprintf (buffer, "%g", p.w);
>   gtk_entry_set_text (GTK_ENTRY (w_entry),buffer);
>   sprintf (buffer, "%g", p.l);
>   gtk_entry_set_text (GTK_ENTRY (l_entry),buffer);
> 
> this works - but the original code was:
> 
>   gtk_entry_set_text (GTK_ENTRY (w_entry),convert_to_char(p.w));  
>   gtk_entry_set_text (GTK_ENTRY (l_entry),convert_to_char(p.l)); 
> 
> and:
> 
> char *convert_to_char(double data)
> {
>   char *c;
>   static char buffer[10];
>   
>   sprintf (buffer, "%g", data);
>   if(data==N_A)
>     sprintf (buffer, "N/A"); 
>     c=buffer;
>     /* printf("buffer:%g\n",data); */
>     
>   return c;  
> }
> 
> It's the convert_to_char function which is screwy, obviously.

Yeah, I think what's happening (note: I'm not a very good coder, so could
very well be wrong) is that buffer[10] is getting freed when
convert_to_char returns, so *c is going out of scope...

> Any ideas to implement this differently?
 
I would g_malloc (or malloc) the buffer instead of using an array, and
have *c point to that...



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