Results 1 to 4 of 4
Thread: Gtk and malloc/realloc woes...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- Jan 2006
Gtk and malloc/realloc woes...
I'm writing an apt-compatible dependency viewer/manager with a Gtk frontend. Many of the command line functions like viewing the recursive dependencies of a package are already functioning fine separate from the Gtk side of things...
To be brief, the program parses the dpkg status file and builds a dependency tree with a rather simple struct. This is dynamically allocated with malloc and resized accordingly with realloc. This all works fine when run out of a console.
However, in the Gtk half of the program, I get random segfaults and malloc errors when the realloc command is executed on a pointer to an array of this struct. The problem can be fixed by using malloc() to choose a static size for this struct-array instead of calling realloc repeatedly. But I'd really like this to be dynamic... wasting thousands of entries for a small rendering job is not ideal.
Am I missing something?
If this command is executed before a gtk_init(), it functions just fine and reallocs the memory repeatedly as the array-struct grows... it seems like the gtk_init does something to mess this up... but I'm stumped..
- Join Date
- Aug 2005
What kind of error print out your application? We should have some code, also.
Are you sure there are no develepo packages to work with APT? (libapt*) Parsing packages should be a bad and big work...When using Windows, have you ever told "Ehi... do your business?"
Linux user #396597 (http://counter.li.org)
Are you dealing with threads? If it is the case, you should properly lock threads to prevent race conditions (use gdk_threads_enter and gdk_threads_leave). This can be the source of your random segfaults.
Using malloc and then realloc is not a clean programming practice. If your are going to allocate structs dynamically you could use GSList or GList. They provide append and remove methods that makes the work for you.
To conclude (this is only a suggestion): malloc and realloc are libc functions. Glib provides g_malloc and similar functions. They works in the same way but they are portable and in some cases safer than the libc equivalents. If you want to develop a Gtk/GNOME application it is a good idea not mixing API's.
- Join Date
- Jul 2004
fernape has already said most of the things I would have said. GTK+ provides data structures to handle what you are describing here. You might want to look into them.
Another thing is that the documentation specifically mentions that one should not mix ordinary malloc and g_malloc, and that one should only use g_free with g_malloc and free with malloc. Mixing these calls can lead to unpredictable problems. Make sure your code is clean in this regard.