Re: structures pointers confusion



On 18 Jul 2001 09:11:21 -0400, Delbert Martin wrote:
> I am working on a new ICQ client. I am trying to pass some login and password values to a function. 
> I am still having problems with this
> 
> my code basically looks similar
> I have a window with two text boxes and two buttons. one button is a login button. the other is an exit button
> the exis button works fine. when i press the login button i want it to take the information typed into the two text
> entries and place it in a structure and send the structure over to a callback where i can write the code to actually
> log the person into the server. most everything is a callbcak function. the main program simply calls the login screen which
> is just a function. 
> 
> /* callbacks file */
> 
> struct info {
> 	GtkWidget *UID;
> 	GtkWIdget *PASS;
> 	}
> 
> gint do_login(GtkWidget *widget, struct info *myInfo) {
> 	gchar *user,*pass;
> 	user = gtk_entry_get_text(myInfo->UID);
> 	pass = gtk_entry_get_text(myInfo->PASS);
> 	/* print output to screen for now */
> 	.........
> 	}
First of all don't use gtk_entry_get_text, instead use
gtk_editable_get_chars. --> user =
gtk_editable_get_chars(GTK_EDITABLE(myInfo->UID),0,-1);

> 
> void login_screen(void) {
> 	......
> 	struct info lgInfo;
> 	lgInfo.UID = gtk_entry_new();
> 	lgInfo.PASS = gtk_entry_new();
> 
> 	.........
> 	gtk_signal_connect(GTK_OBJECT(login_button),"clicked",GTK_SIGNAL_FUNC(do_login), &myInfo );
> 	........
> 	}
> 

The struct variabele won't exist anymore at the end of this function, so
you're passing a pointer to unallocated memory to the callback function,
which will screw up your entire program. Two sollutions: 1) use a global
variable or 2) allocate memory for the structure and pass that pointer
to the callback function, free the struct when you're ready (preffered).

So something like this:
struct info * lgInfo = g_malloc(sizeof(struct info));
gtk_signal_connect(...., lgInfo);

and when you don't need the struct anymore: g_free(lgInfo);


> /* main program */
> 
> int main(......) {
> 	gtk_init(......);
> 	login();
> 	gtk_main();
> 	return 0;
> 	}
> 
> any help possible would be greatly appreciated--Delbert Martin IV
> 
> 
> 
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list

Try to find some information about memory allocation and pointers in C.
This is really essential to know well when programming C, and a source
of many many bugs....

Greetz,

--
Jeroen Benckhuijsen

Software Engineer
Phoenix Software




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