From: Lars Rune Nøstdal
Subject: floating point error when messing with GTK+
Date: 
Message-ID: <pan.2006.12.21.13.12.58.783782@gmail.com>
Hey,
I'm messing with GTK+ and was wondering if anyone has any idea why this
happens and/or how to handle or debug this:


(eval-when (:load-toplevel :execute :compile-toplevel)
  (require :cffi))

(eval-when (:load-toplevel :execute :compile-toplevel)
  (cffi:load-foreign-library "libgtk-x11-2.0.so")
  (cffi:load-foreign-library "libgdk-x11-2.0.so")
  (cffi:load-foreign-library "libglib-2.0.so")
  (cffi:load-foreign-library "libgthread-2.0.so"))


(defpackage :test
  (:use :cl :cffi))
(in-package :test)


(defun latestBug ()
  (sb-thread:make-thread
   (lambda ()
     (foreign-funcall "g_thread_init" :pointer (null-pointer) :void)
     (foreign-funcall "gdk_threads_init" :void)
     (foreign-funcall "gtk_init" :pointer (null-pointer) :pointer (null-pointer) :void)
     (foreign-funcall "gdk_threads_enter" :void)
     (foreign-funcall "gtk_main" :void)
     (foreign-funcall "gdk_threads_leave" :void)))

  (sleep 1) ;; Make sure thread hast started `gtk_main'.
  
  (foreign-funcall "gdk_threads_enter" :void)
  (let ((window (foreign-funcall "gtk_window_new" :int 1 :pointer))
        (hscale
         (foreign-funcall "g_object_new" :int
                          (foreign-funcall "gtk_hscale_get_type" :int)
                          :pointer (null-pointer) :pointer)))
    (foreign-funcall "gtk_container_add" :pointer window :pointer hscale :void)
    (foreign-funcall "gtk_widget_show_all" :pointer window :void))
  (foreign-funcall "gdk_threads_leave" :void))



;;; I get this:

#|
arithmetic error floating-point-invalid-operation signalled
   [Condition of type floating-point-invalid-operation]

Restarts:
  0: [terminate-thread] Terminate this thread (#<thread {B41D5B1}>)

Backtrace:
  0: ((flet #:g153))
  1: (sb-vm:sigfpe-handler #<unavailable argument> #.(sb-sys:int-sap #XB6050F3C) #<unavailable argument>)
  2: (sb-sys:invoke-interruption #<CLOSURE (lambda nil) {B4220CD}>)
  3: ("foreign function: call_into_lisp")
  4: ("foreign function: funcall3")
  5: ("foreign function: interrupt_handle_now")
  6: ("bogus stack frame")
  7: ("foreign function: gtk_paint_box")
  8: ("foreign function: #xB5C6D078")
  9: ("foreign function: #xB5BEF469")
 10: ("foreign function: _gtk_marshal_BOOLEAN__BOXED")
 11: ("foreign function: #xB5901FB9")
 12: ("foreign function: g_closure_invoke")
 13: ("foreign function: #xB59141E3")
 14: ("foreign function: __pthread_mutex_unlock")
 15: ("foreign function: #x0")
|#



;;; What seems(?) to be similar C-code works ok:

#|
#include <gtk/gtk.h>
#include <pthread.h>
#include <unistd.h>

void* start(void* arg)
{
  g_thread_init(NULL);
  gdk_threads_init();
  gtk_init(NULL, NULL);
  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();
  return NULL;
}


int main(int argc, char* argv[])
{
  pthread_t thread;
  pthread_create(&thread, NULL, &start, NULL);
  printf("sleeping ...\n");
  
  sleep(1);
  
  gdk_threads_enter();
  GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  GtkWidget* hscale = g_object_new(gtk_hscale_get_type(), NULL);
  gtk_container_add(GTK_CONTAINER(window), hscale);
  gtk_widget_show_all(window);
  gdk_threads_leave();

  while(1) {
    printf("main(): Still alive ...\n");
    sleep(1);
  }
    
  return 0;
}
|#


I'm using SBCL-1.0 under Ubuntu Edgy (x86).

-- 
Lars Rune Nøstdal
http://nostdal.org/

From: Luigi Panzeri
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <87psadfi7a.fsf@matley.muppetslab.org>
Your code works on my laptop (sbcl 1.0, GTK 2.8).

The constant for GTK_WINDOW_TOPLEVEL is 0 not 1 afaik.



-- 
Luigi Panzeri aka Matley

Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Evaluate Lisp: http://lisp.tech.coop/Evaluate%20Lisp
From: Lars Rune Nøstdal
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <pan.2006.12.21.14.26.05.536089@gmail.com>
On Thu, 21 Dec 2006 15:15:53 +0100, Luigi Panzeri wrote:

> Your code works on my laptop (sbcl 1.0, GTK 2.8).
> 
> The constant for GTK_WINDOW_TOPLEVEL is 0 not 1 afaik.

Yes; looking at gtkenums.h this seems right. I've also discovered that
sometimes a call to `gdk_flush' is needed for things to get going:

    (foreign-funcall "gtk_container_add" :pointer window :pointer hscale :void)
    (foreign-funcall "gtk_widget_show_all" :pointer window :void))
  (foreign-funcall "gdk_flush" :void)
  (foreign-funcall "gdk_threads_leave" :void))

..i still get this floating point error though.

-- 
Lars Rune Nøstdal
http://nostdal.org/
From: Lars Rune Nøstdal
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <pan.2006.12.21.16.22.39.425683@gmail.com>
I also get the floating point exception for this simple code:


(eval-when (:compile-toplevel)
  (require :cffi))

(eval-when (:compile-toplevel)
  (cffi:load-foreign-library "libgtk-x11-2.0.so")
  (cffi:load-foreign-library "libgdk-x11-2.0.so")
  (cffi:load-foreign-library "libglib-2.0.so")
  (cffi:load-foreign-library "libgthread-2.0.so"))


(defpackage :test
  (:use :cl :cffi))
(in-package :test)


(defun latestBug ()
  (foreign-funcall "gtk_init" :pointer (null-pointer) :pointer (null-pointer) :void)
  (let ((window (foreign-funcall "gtk_window_new" :int 0 :pointer))
        (hscale (foreign-funcall "g_object_new"
                                 :int (foreign-funcall "gtk_hscale_get_type" :int)
                                 :pointer (null-pointer) :pointer)))
    (foreign-funcall "gtk_container_add" :pointer window :pointer hscale :void)
    (foreign-funcall "gtk_widget_show_all" :pointer window :void))
  (foreign-funcall "gtk_main" :void))

..weird..

-- 
Lars Rune Nøstdal
http://nostdal.org/
From: Lars Rune Nøstdal
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <pan.2006.12.21.16.38.08.861812@gmail.com>
Just tried this at a second machine - Dapper with GTK+-2.8 -- same thing.

-- 
Lars Rune Nøstdal
http://nostdal.org/
From: ·············@gmail.com
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <1166721370.707988.99530@80g2000cwy.googlegroups.com>
Have no idea it is related error (my Linux machine is away from me
right now), but have you seen this thread?

http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/9abc595b2292961/703eaf1bae030709


Lars Rune Nøstdal wrote:
> Just tried this at a second machine - Dapper with GTK+-2.8 -- same thing.
> 
> -- 
> Lars Rune Nøstdal
> http://nostdal.org/
From: Lars Rune Nøstdal
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <pan.2006.12.21.17.33.10.635547@gmail.com>
On Thu, 21 Dec 2006 09:16:10 -0800, ·············@gmail.com wrote:

> Have no idea it is related error (my Linux machine is away from me
> right now), but have you seen this thread?
> 
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/9abc595b2292961/703eaf1bae030709


Yes! That fixed it:


(eval-when (:compile-toplevel)
  (require :cffi))

(eval-when (:compile-toplevel)
  (cffi:load-foreign-library "libgtk-x11-2.0.so")
  ;(cffi:load-foreign-library "libgdk-x11-2.0.so")
  ;(cffi:load-foreign-library "libglib-2.0.so")
  )

(defpackage :test
  (:use :cl :cffi))
(in-package :test)


(defun latestBug ()
  (sb-int:with-float-traps-masked  (:invalid :divide-by-zero)
    (foreign-funcall "gtk_init" :pointer (null-pointer) :pointer (null-pointer) :void)
    (let ((window (foreign-funcall "gtk_window_new" :int 0 :pointer))
          (hscale (foreign-funcall "g_object_new"
                                   :int (foreign-funcall "gtk_hscale_get_type" :int)
                                   :pointer (null-pointer) :pointer)))
      (foreign-funcall "gtk_container_add" :pointer window :pointer hscale :void)
      (foreign-funcall "gtk_widget_show_all" :pointer window :void))
    (foreign-funcall "gtk_main" :void)))


Works ok; thank you. :D


-- 
Lars Rune Nøstdal
http://nostdal.org/
From: ·············@gmail.com
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <1166726153.919189.161120@73g2000cwn.googlegroups.com>
Apparently a lot of C code in the wild is pretty lax with divisions by
zero, etc...

> Yes! That fixed it:
>
From: Lars Rune Nøstdal
Subject: Re: floating point error when messing with GTK+
Date: 
Message-ID: <pan.2006.12.21.19.06.58.311257@gmail.com>
On Thu, 21 Dec 2006 10:35:54 -0800, ·············@gmail.com wrote:

> Apparently a lot of C code in the wild is pretty lax with divisions by
> zero, etc...

Yeah; this doesn't seem like a "good thing".

-- 
Lars Rune Nøstdal
http://nostdal.org/