On Apr 7, 2009, at 11:30, Bertram Felgenhauer wrote:

> Axel Simon wrote:
> [...]
>> This GDB was configured as "x86_64-redhat-linux-gnu"...
>> Using host libthread_db library "/lib64/libthread_db.so.1".
>> (gdb) run
>> [Thread debugging using libthread_db enabled]
>> [New Thread 46912496453984 (LWP 12364)]
>> [New Thread 1084229968 (LWP 12367)]
>> [New Thread 1094719824 (LWP 12368)]
>>
>> Program received signal SIGSEGV, Segmentation fault.
>
> Is this easy to reproduce for you? It doesn't seem to crash for me,
> but that's on an uniprocessor x86 computer.

I think it does this every time. Note that this is with the ghc 6.10  
snapshot with the Gtk2Hs patch that lets finalizers run as threads.  
It might just be a NULL pointer that is being freed. Even then, the  
function pointer being freed should never be NULL.

>> [Switching to Thread 46912496453984 (LWP 12364)]
>> 0x0000000000439a71 in freeHaskellFunctionPtr ()
>
> freeHaskellFunctionPtr corresponds directly to freeHaskellFunPtr in
> Foreign.Ptr; the RTS itself doesn't call it.

Ok. Since the patched Gtk2Hs frees function pointers as before, you  
list is valid for the patched and the unpatched version of Gtk2Hs.

> So I've looked into places that call freeHaskellFunPtr in gtk2hs.
> There are a few common idioms, a few oddballs and an actual bug in  
> gio.
>
> Idiom 1:
>   This idiom creates a FunPtr that cleans up itself when it's being  
> run.
>   Those FunPtrs may be called only once; they're usually added as
>   destroy notifiers.
>
>   Example:
>     dRef <- newIORef nullFunPtr
>     dPtr <- mkDestructor $ do    -- some foreign wrapper
>       freeHaskellFunPtr hPtr     -- varies
>       dPtr <- readIORef dRef
>       freeHaskellFunPtr dPtr
>     writeIORef dRef dPtr
>
> Idiom 2:
>   This idiom creates a temporary FunPtr to be called by C immediately,
>   and frees it afterwards. It comes in two variants:
>
>   Variant a:
>     bracket (directoryVisitCallbackMarshal callback) -- ! foreign  
> wrapper
>             freeHaskellFunPtr                        -- !
>             ...
>
>   Variant b:
>     cProgressCallback <- xferProgressCallbackMarshal ...
>     cResult <- cXfer ...                                 -- varies
>     freeHaskellFunPtr cProgressCallback
>
>   Variant c: see below.
>
> Idiom 3:
>   This is similar to Idiom 1, but instead of creating a temporary
>   IORef to hold the FunPtr to be freed, it's instead passed as user
>   data by gtk.
>
>   Example:
>     marshalAsyncReadyCallback ... =
>       makeAsyncReadyCallback cAsyncReadyCallback -- foreign wrapper
>       where cAsyncReadyCallback cObject cAsyncResult cCallback = do
>             ...
>             freeHaskellFunPtr (castPtrToFunPtr cCallback)
>
>     used like this:
>       g_file_read_async cFile ... (castFunPtrToPtr cCallback)
>
> Oddballs:
>   gnomevfs/System/Gnome/VFS/Marshal.chs:
>     volumeOpCallbackMarshal makes an odd use of 'finally' (The code is
>     a variant of idiom 3):
>
>       let cCallbackFunPtr = castPtrToFunPtr cUserData
>       in  finally (freeHaskellFunPtr cCallbackFunPtr) $ ...
>
>     As far as I can see, freeHaskellFunPtr doesn't throw exceptions  
> (it
>     may, however, crash), so that use of 'finally' is useless.

Doesn't the finally clause assure that freeHaskellFunPtr is called  
even if the code after the $ throws an exception?

>   gnomevfs/System/Gnome/VFS/Monitor.chs:
>     monitorCancel simply frees a FunPtr which was allocated by  
> monitorAdd.
>     Note that calling monitorCancel twice with the same handle is not
>     possible.
>
>   tools/c2hs/doc/c2hs/lib/Ptr.hs:
>     This file seems unused. It may have worked on Hugs once upon a  
> time.

Yes, we don't use any of the c2hs supplied utilities. We could  
actually remove these files from our c2hs version.

>   gstreamer/Media/Streaming/GStreamer/Core/Bus.chs.pp:
>     The code keeps track of a 'sync handler' for a bus. There are two
>     functions that may free it:
>
>     unsetSyncHandler:
>       oldWeakNotifyM <- getWeakNotify bus
>       case oldFunPtrM of
>         Just oldFunPtr -> freeHaskellFunPtr oldFunPtr
>         Nothing        -> return ()
>
>     busSetSyncHandler:
>       weakNotify <- objectWeakref bus $ freeHaskellFunPtr funPtr
>       setWeakNotify bus $ Just weakNotify
>
> Bugs:
>   gio/System/GIO/File.chs.pp:
>     The code looks like this:
>
>     cProgressCallback <- maybe (return nullFunPtr)
>         marshalFileProgressCallback progressCallback
>     propagateGError $ \cError -> do
>         ret <- g_file_copy cSource ... cError
>         freeHaskellFunPtr cProgressCallback
>
>     This may call freeHaskellFunPtr with nullFunPtr as its  
> argument, which
>     leads to a crash.
>
>     This is variant c of idiom 2.
>
> Sadly, none of this seems to be related to the ProgressThreadedRTS  
> demo.

I haven't had the time to investigate. It might be trivial. Or it  
might be a but in GHC. Anywhere between these two extremes.

Thanks for the list, it might indeed come in handy if/when we're  
changing the use of finalizers to the immediately run C finalizers.  
This might not be possible at all since objectWeakref allows you to  
add a callback that is called when the object is destroyed (which is  
exactly the forbidden way to enter the GHC runtime).

Cheers,
Axel.

> Bertram
>
>
> Appendix: Overview of files using freeHaskellFunPtr
>
> gconf/System/Gnome/GConf/GConfClient.chs
>   connect_GConfClientNotifyFunc (idiom 1)
>
> glib/System/Glib/GObject.chs.pp
>   mkFunPtrDestroyNotify (idiom 1)
>   objectWeakref (idiom 1)
>   objectSetAttribute (idiom 1)
>
> glib/System/Glib/Signals.chs.pp
>   mkFunPtrClosureNotify (idiom 1)
>
> gnomevfs/System/Gnome/VFS/Directory.chs
>   directoryVisitMarshal (idiom 2a)
>
> gnomevfs/System/Gnome/VFS/Marshal.chs
>   volumeOpCallbackMarshal (oddball, idiom 3)
>
> gnomevfs/System/Gnome/VFS/Monitor.chs
>   monitorCancel (oddball)
>
> gnomevfs/System/Gnome/VFS/Xfer.chs.pp
>   marshalXfer (idiom 2b)
>
> gstreamer/Media/Streaming/GStreamer/Core/Iterator.chs
>   iteratorFold (idiom 2b)
>
> gstreamer/Media/Streaming/GStreamer/Core/Bus.chs.pp
>   unsetSyncHandler (oddball)
>   busSetSyncHandler (oddball)
>
> gtk/Graphics/UI/Gtk/Abstract/Container.chs.pp
>   containerForeach (idiom 2b)
>   containerForall (idiom 2b)
>
> gtk/Graphics/UI/Gtk/General/Clipboard.chs.pp
>   mkFunPtrClearFunc (idiom 1)
>   clipboardRequestContents (idiom 1)
>   clipboardRequestImage (idiom 1)
>   clipboardRequestTargets (idiom 1)
>   clipboardRequestRichText (idiom 1)
>
> gtk/Graphics/UI/Gtk/ModelView/IconView.chs.pp
>   iconViewSelectedForeach (idiom 2b)
>
> gtk/Graphics/UI/Gtk/ModelView/TreeModel.chs.pp
>   treeModelForeach (idiom 2b)
>
> gtk/Graphics/UI/Gtk/ModelView/TreeSelection.chs.pp
>   treeSelectionSelectedForeach (idiom 2b)
>
> gtk/Graphics/UI/Gtk/ModelView/TreeView.chs.pp
>   treeViewMapExpandedRows (idiom 2b)
>
> gtk/Graphics/UI/Gtk/Multiline/TextIter.chs.pp
>   textIterForwardFindChar (idiom 2b)
>   textIterBackwardFindChar (idiom 2b)
>
> gtk/Graphics/UI/Gtk/TreeList/IconView.chs.pp
>   iconViewSelectedForeach (idiom 2b)
>
> gtk/Graphics/UI/Gtk/TreeList/TreeModel.chs.pp
>   treeModelForeach (idiom 2b)
>
> gtk/Graphics/UI/Gtk/TreeList/TreeSelection.chs.pp
>   treeSelectionSelectedForeach (idiom 2b)
>
> gtk/Graphics/UI/Gtk/TreeList/TreeView.chs.pp
>   treeViewSetColumnDragFunction (idiom 2b)
>   treeViewMapExpandedRows (idiom 2b)
>
> gtk/Graphics/UI/Gtk/Cairo.chs.pp
>   pixbufFromImageSurface (idiom 1)
>
> gio/System/GIO/Base.chs
>   marshalAsyncReadyCallback (idiom 3)
>
> gio/System/GIO/File.chs.pp
>    fileCopy (idiom 2c)
>   fileCopyAsync (idiom 2c)
>   fileMove (idiom 2c)
>
> tools/c2hs/doc/c2hs/lib/Ptr.hs
>   unused file.
>
> ---------------------------------------------------------------------- 
> --------
> This SF.net email is sponsored by:
> High Quality Requirements in a Collaborative Environment.
> Download a free trial of Rational Requirements Composer Now!
> http://p.sf.net/sfu/www-ibm-com
> _______________________________________________
> Gtk2hs-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/gtk2hs-devel


------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Gtk2hs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtk2hs-devel

Reply via email to