Reputation:
I'm still having problems in my GTK program.
When I click the encode (or decode) button I get the error message:
Segmentation Fault
I think the problem comes from functions: ret
and encode
but can't find it.
//..
//gets the first argument(buf)
tx1=gtk_entry_get_text(GTK_ENTRY(entry));
//...
//gets the second argument(pass)
tx2=gtk_entry_get_text(GTK_ENTRY(entry));
//....
//signal to callback function encode when button clicked
g_signal_connect(but,"clicked",G_CALLBACK(encode(tx1,tx2)),NULL);
// ...
//convert const char* to char*
char* ret (const char *bd){
char *c = new char[12];
strcpy(c,bd);
return c;
}
//encode function
char encode(const char ebuf[],const char epass[]) {
//This is the complete function I wrote and I am still having the error.
char *buf=ret(ebuf);
char *pass=ret(epass);
}
Upvotes: 0
Views: 368
Reputation: 98516
This line makes no sense:
g_signal_connect(but,"clicked",G_CALLBACK(encode(tx1,tx2)),NULL);
You are calling encode()
with some arguments, it returns a char
value, (undefined by the way) and you cast that char
into a GCallback
, that is a pointer-to-function. Then, when the button is clicked Gtk+ will try to call your char
which is obviously not a valid function and hence the segmentation fault.
Not sure what you want to do, but you have to play by the rules, and the rules say that the "clicked"
callback should be a pointer-to-function with this prototype:
void f(GtkButton *button, gpointer user_data);
Other than that and you are playing really dangerous.
EXAMPLE CODE AHEAD
Say, for example that you want to call function encode
with a char*
as argument. You could do it like this:
void clicked_encode(GtkButton *button, gpointer user_data)
{
char *d = static_cast<char*>(user_data);
encode(d);
}
And to register the callback
g_signal_connect(but, "clicked", G_CALLBACK(clicked_encode), text);
That assumes that the memory pointed to by text
lives at least as long as the callback. If that is not the case, you make a copy, but then you have to remember to free it:
char *d = strdup(text);
g_signal_connect_data(but, "clicked", G_CALLBACK(clicked_encode), d, free_text, 0);
//and the deleter function
void free_text(gpointer *data, GClosure *)
{
char *d = static_cast<char*>(data);
free(d);
}
Naturally, you may need more than one parameter in your function, but you can only pass one pointer. The solution is to define a struct (or a class, you are using C++) with everything in it:
FancyClass *d = new FancyClass(...);
g_signal_connect_data(but, "clicked", G_CALLBACK(clicked_encode), d, free_fancy_class, 0);
void clicked_encode(GtkButton *button, gpointer user_data)
{
FancyClass *d = static_cast<FancyClass*>(user_data);
encode(d);
}
void free_fancy_class(gpointer *data, GClosure *)
{
FancyClass *d = static_cast<FancyClass*>(data);
delete d;
}
Hey! You can even make encode()
a member function of FancyClass
. The possibilities are endless!
I hope thit will put you on the right track...
Upvotes: 1
Reputation: 204
If you do not want to have more dynamically sized character strings, use strncpy to limit the number of characters to be copied.
Upvotes: 0