I'm trying to get familiar with Haskell's FFI so I wrote this small example:
Main.hs:
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign.C.Types
import Foreign.Ptr (Ptr)
import Foreign.Marshal.Array (peekArray)
import Foreign.Marshal.Alloc (free)
foreign import ccall "test.h test"
test :: CInt -> Ptr CInt
main = do
let rval = test 6
-- print the array
list <- peekArray 6 rval >>= return . map toInteger
putStrLn $ show list
free rval
-- print it again (it should okay)
putStrLn $ show list
-- try to retrieve it AGAIN after I used free. Should print random values
peekArray 6 rval >>= return . map toInteger >>= putStrLn . show
test.h
#ifndef TEST_H
#define TEST_H
int* test(int a);
#endif
test.c
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
int* test(int a)
{
int* r_val = (int*)malloc(a * sizeof(int));
r_val[0] = 1;
r_val[1] = 2;
r_val[2] = 3;
r_val[3] = 4;
r_val[4] = 5;
r_val[5] = 6;
return r_val;
}
The output I get when I compile and run Main.hs
is:
D:\Code\Haskell\Projects\Dev\TestFFI>cabal build
Building TestFFI-0.1.0.0...
Preprocessing executable 'TestFFI' for TestFFI-0.1.0.0...
[1 of 1] Compiling Main ( src\Main.hs, dist\build\TestFFI\TestFFI-tmp\Main.o )
Linking dist\build\TestFFI\TestFFI.exe ...
D:\Code\Haskell\Projects\Dev\TestFFI>
D:\Code\Haskell\Projects\Dev\TestFFI>dist\build\TestFFI\TestFFI.exe
[1,2,3,4,5,6]
[1,2,3,4,5,6]
[1,2,3,4,5,6]
That doesn't seem to make any sense to me. The 3rd time I printed the array I was expecting something like:
[69128391783,2083719073,934857983457,98374293874,0239823947,2390847289347]
random data!
Am I doing something wrong? Have I missed something?
Reading memory after you've freed it is undefined behavior. Anything is allowed -- including returning random data, summoning nasal demons, or even, in the boringest of possible worlds, returning the data that was in that memory before it was freed.