When I return a value from a Haskell function called from C, in
some cases the program SIGSEGVs. I guess it works when the value is
"already computed", but fails otherwise. A compiler bug or my error?
This is ghc-4.02, egcs-1.1.1.

[qrczak ~/haskell]$ cat Main.hs
{-# OPTIONS -#include "Mc.h" #-}
module Main (main) where
import Addr (Addr)
call_h :: Int -> IO Int
call_h n = do
    putStrLn $ show n
    return (n + 1) -- Here when I return simply 5 or n, it works!
    -- But it does not help to use seq:
    -- let k = n + 1; k `seq` return k
main :: IO ()
main = do
    putStrLn "BEFORE"
    h <- ch call_h
    call_c h
    putStrLn "AFTER"
foreign import "call_c" call_c :: Addr -> IO ()
foreign export dynamic ch :: (Int -> IO Int) -> IO Addr

[qrczak ~/haskell]$ cat Mc.h
void call_c (int (*f)(int));

[qrczak ~/haskell]$ cat Mc.c
#include <stdio.h>
void call_c (int (*f)(int))
{
    int x;
    printf ("before\n");
    x = f (6);
    f (x);
    printf ("after\n");
}

[qrczak ~/haskell]$ ghc -c Main.hs -fglasgow-exts
ghc: module version changed to 1; reason: no old .hi file
[qrczak ~/haskell]$ gcc -c Mc.c
[qrczak ~/haskell]$ ghc Main.o Main_stub.o Mc.o -o M
[qrczak ~/haskell]$ ./M
BEFORE
before
6
Segmentation fault
[qrczak ~/haskell]$ 

-- 
 __("<    Marcin Kowalczyk * [EMAIL PROTECTED] http://kki.net.pl/qrczak/
 \__/          GCS/M d- s+:-- a22 C+++>+++$ UL++>++++$ P+++ L++>++++$ E-
  ^^                W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP->+ t
QRCZAK                  5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-

Reply via email to