These are some hints how to avoid common problems in your applications.
- Disconnect handlers
- Objects cannot be freed when there are
still signal handlers connected. On widgets, you can call the method
destroy(), which disconnects all handlers, destroys child
widgets and removes itself from parents. For non-widgets, you can
remember the handler_ids returned from the connect call like this:
handler_id = obj:connect('signal-name', signal_handler) obj:disconnect(handler_id)
- Retrieving values from pointer arguments
- Functions sometimes expect a pointer to a memory location where they
can place a return value. If you specify nil for that argument, a
NULL pointer is given to the function, which normally means that the return
value should not be stored, rather discarded.
For int* and similar types, you should simply give a value to initialize the memory location with; the function can then overwrite it and it will be returned to the caller.
For double pointer to enum arguments you should specify gnome.NIL instead of nil, like in the following example:
-- call gchar* g_file_read_link(const gchar*, GError**) s, err = glib.file_read_link(path, gnome.NIL) if err then error(err.message) end
- Wrong data type for vararg
- When converting Lua values to call a library function with variable
arguments, sometimes the wrong data type is used. For example, consider
In this case. an integer is given to printf, which actually expects a double. To avoid this, use a Boxed Value with a type cast, like this:
glib.printf("%f\\n", gnome.box(20, "double"))