[gambit-list] Segfault while importing enum

Marc Feeley feeley at iro.umontreal.ca
Wed Nov 2 13:39:31 EDT 2011


On 2011-11-02, at 12:05 PM, Vok Vojwo wrote:

> I tried to import a Gtk enumeration constant:
> 
> ;; Local Variables:
> ;; compile-command: "gsc -cc-options \"`pkg-config --cflags
> gtk+-2.0`\" -ld-options \"`pkg-config --libs gtk+-2.0`\" -exe
> enum.scm"
> ;; End:
> 
> (c-declare "#include <gtk/gtk.h>")
> 
> (c-define-type GtkWindowType "GtkWindowType")
> 
> (define gtk-window-toplevel
>  ((c-lambda () GtkWindowType "___result = GTK_WINDOW_TOPLEVEL;")))
> 
> (write gtk-window-toplevel)
> 
> But I get a segmentation fault when I run the program.
> 
> When I use int instead of the enum type the code does not segfault:
> 
> (define gtk-window-toplevel
>  ((c-lambda () int "___result = GTK_WINDOW_TOPLEVEL;")))
> 
> Does this mean, that I have to ignore all C enum types and treat them as ints?

In C, an enum is an int.  In the FFI code, you have to be precise about the concrete type so that the C interface can do the appropriate conversions between C and Scheme.  When you say

(c-define-type GtkWindowType "GtkWindowType")

the Gambit compiler has an abstract knowledge of that type, and no knowledge of its representation.  So you have to say instead:

(c-define-type GtkWindowType int)

and then you can use it like this:

(define gtk-window-toplevel
 ((c-lambda () GtkWindowType "___result = GTK_WINDOW_TOPLEVEL;")))

Alternatively, use this macro to import C enums and constants:

(define-macro (import-int-constants . names)
  `(begin
     ,@(map (lambda (name)
              `(define ,name
                 ((c-lambda () int ,(string-append "___result = " (symbol->string name) ";")))))
            names)))

(import-int-constants
 GTK_WINDOW_TOPLEVEL
)

> The documentation for c-define-type does not say much about enumerations:
> 
> http://www.iro.umontreal.ca/~gambit/doc/gambit-c.html#c_002ddefine_002dtype

I'll add something to the doc to explain this.

Marc




More information about the Gambit-list mailing list