My program is crashing - help!

Marcin 'Qrczak' Kowalczyk [email protected]
Tue, 2 Jan 2001 17:43:25 +0100 (CET)


I don't know if this is a bug in my code, in ghc, or in the python
interpreter. I could not find it for a long time. Would somebody please
help me?

In <http://qrczak.ids.net.pl/HsPython-bug.tar.gz> there is a simplest
fragment I could ake which exposes the bug. It requires ghc-4.11 and
python-2.0. It should be tweakable for older versions of both:

- For older ghc versions, either replace Ptr/FunPtr with Addr and
  ForeignPtr with ForeignObj, or use QForeign instead of Foreign.
  In any case you must install hsc2hs from ghc >= 4.09 or QForeign.
  QForeign is at
  <http://download.sourceforge.net/qforeign/qforeign-0.63.tar.gz>.

- For older Python versions, fix paths in the Makefile.

The program should do an equivalent of
    main = putStr (repeat '9')
but in a very indirect way, calling Python which calls back Haskell for
each digit. On my machine it crashes after several lines of output (inside
malloc).

It is very sensitive to small changes. When compiled with -O, it works
fine. When the PyObject module is merged with Main, it works fine. When
nullFunPtr is passed in Main.hsFunctionNew instead of freeMethodDefPtr, it
still crashes, but if nullFunPtr is replaced with 0 and the corresponding
type in a foreign import is changed to Int, it works fine (well, this
applied to a version before removing unnecessary junk, now it may be
different). When freeMethodDef is written in C and foreign imported
instead of being written in Haskell and foreign exported, it works fine.
Et caetera.

I tried to run it under ghc-4.08.1 and python-1.5 (after necessary
tweaking), but it's very different: it crashes very soon and ltrace shows
that a message about passing a non-tuple as arguments is strcpy'ed just
before the crash. Perhaps the crash is expected, since I don't catch
Python exceptions in the simplified version, but the message is not: I am
passing the result of PyTuple_New(0) (after wrapping in a ForeignPtr).
Anyway, it is very sensitive to small changes of compilers or the source
itself.

Maybe I am using the Python interface in a wrong way, or I am doing some
stupid mistake, or there is a bug in ghc - I really don't know what is
going on. Help!

-- 
Marcin 'Qrczak' Kowalczyk