Hello,<br>I'm trying to use a C++ class in Haskell through C exports.<br>It works all very well, except that when I call the function that deletes the object, it is still valid, I can still call methods on it.<br><br>Here is my Haskell code:<br>
<font face="courier new,monospace"><br>
{-# LANGUAGE ForeignFunctionInterface #-}<br>
<br>
import Foreign<br>
import Foreign.C.Types<br>
<br>
newtype PKlass = PKlass (Ptr PKlass)<br>
<br>
foreign import ccall unsafe "Klass_Create"<br>
kCreate :: CInt -> CInt -> IO PKlass<br>
<br>
foreign import ccall unsafe "Klass_Destroy"<br>
kDestroy :: PKlass -> IO ()<br>
<br>
foreign import ccall unsafe "Klass_GetX"<br>
kGetX :: PKlass -> IO CInt<br>
foreign import ccall unsafe "Klass_GetY"<br>
kGetY :: PKlass -> IO CInt<br>
<br>
foreign import ccall unsafe "Klass_AddKlass"<br>
kAdd :: PKlass -> PKlass -> IO PKlass<br>
<br>
<br>
main = do<br>
k <- kCreate 4 5<br>
kDestroy k<br>
<b><span style="color: rgb(255, 0, 0);">kGetY k >>= print -- This shouldn't work</span></b><br>
k' <- kCreate 2 8<br>
k'' <- k `kAdd` k'<br>
kDestroy k''<br>
<b><span style="color: rgb(255, 0, 0);">kGetY k'' >>= print -- This neither</span></b><br><br><br><font face="arial,helvetica,sans-serif">So it is very basic, and I can't understand why the supposedly destroyed objects are still here.<br>
Enclosed is all the source code (C++ class, C exportation and the Haskell main hereabove).<br><br>I compile it this way:<br>ghc --make main.hs *.cpp -lstdc++<br></font></font>