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:
<%= inline_code [[
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:
<%= inline_code [[
-- 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
this:
<%= inline_code [[
glib.printf("%f\\n", 20)
]] %>
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:
<%= inline_code [[
glib.printf("%f\\n", gnome.box(20, "double"))
]] %>
- Checking a FLAG
- If you want to check whether a FLAG variable has a certain bit set,
you can use the modulo operator with a second argument that is a number or
another FLAG. The number is useful because event.key.state is defined to
be a number, for some reason. So you can try this:
<%= inline_code [[
assert(event.type == gdk.KEY_PRESS or event.type == gdk.KEY_RELEASE)
has_ctrl = gdk.CONTROL_MASK % event.key.state
]] %>