Re: memory problem ?



On Mon, 2001-09-10 at 09:54, danilo lujambio wrote:
Hi, my english is no enough but I will try


in a program I declare a global variable :

gchar *id_cabina_global;
........
.......

in main I allocate :

main( )
{
..........
id_cabina_global = (gchar *)g_malloc(sizeof(LEN_CABINA));
.......
}
in some place :

 id_cabina_global = aux;

here you're re-assigning id_cabina_global, and thus loosing your
reference to the buffer allocated in main()

in one callback function that I use, which creates a new frame with
information

reporte_dialog()
{

  GtkWidget *hbox_freporte;
  GtkWidget *vbox_freporte;

........................

then I need to use

id_cabina_global

and the value is strange ( not the assigned previously)

but if I make :

if ((hbox_freporte = (GtkWidget *)malloc(sizeof(GtkWidget)) == NULL))
      {
     g_print(".....");
     exit(0);
      }

    if ((vbox_freporte = (GtkWidget *)malloc(sizeof(GtkWidget)) ==
NULL))
      {
     g_print("........");
     exit(0);
      }

why are you malloc-ing GtkWidgets? These are usually allocated through
their constructors (_new() functions).

the id_cabina_global value is OK .

Why ?

It's hard to say. When you malloc you mess around with the free-lists
and stuff, but that really shouldn't affect a global pointer, nomatter
what it points to. Now as to changing the thing that it points to, (not
the address but the data there), malloc-ing could easily return a buffer
from inside there, and even if you don't access the returned memory
buffer, the messing around with free-lists could easily change what you
are pointing at.

why I need to use malloc in vbox_freporte and hbox_freporte to preserve
id_cabina ?

you shouldn't.

sorry if my question is stupid but I can't understand the root of
problem

neither can I...perhaps if you track all assignments to the variable,
and print some output about before/after states you would know if it is
changed there. If not, something else is probably pointing the the same
buffer as id_cabina_global and the problem is there.

        /mailund

-- 
And out of the chaos, a voice spoke:
"Smile and be happy, for it can always be worse".
And I smiled, and I was happy, and it did get worse.




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