I assume you are right.<br>The weirdest thing here is that getY() returns the Y value of the destructed object while getX() returns always 0.<br><br><br><div class="gmail_quote">2011/2/26 Miguel Mitrofanov <span dir="ltr">&lt;<a href="mailto:miguelimo38@yandex.ru">miguelimo38@yandex.ru</a>&gt;</span><br>

<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Well, this code in C++ would probably work too:<br>
<br>
Klass *k = new Klass(4,5);<br>
delete k;<br>
std::cout &lt;&lt; k-&gt;getY() &lt;&lt; std::endl;<br>
<br>
though smart compiler would probably issue a warning. See, when you delete something, C++ doesn&#39;t automagically mark your pointer as &quot;invalid&quot;; in fact, it preserves all the data in your deleted class. If you didn&#39;t provide a destructor, then the only outcome of &quot;delete&quot; would be that the same memory can be assigned to another object by &quot;new&quot; operator, but it doesn&#39;t get cleared or invalidated in any way.<br>


<br>
Seems to me, Haskell works in the same way.<br>
<div><div></div><div class="h5"><br>
On 26 Feb 2011, at 13:59, Yves Parès wrote:<br>
<br>
&gt; Hello,<br>
&gt; I&#39;m trying to use a C++ class in Haskell through C exports.<br>
&gt; 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>
&gt;<br>
&gt; Here is my Haskell code:<br>
&gt;<br>
&gt; {-# LANGUAGE ForeignFunctionInterface #-}<br>
&gt;<br>
&gt; import Foreign<br>
&gt; import Foreign.C.Types<br>
&gt;<br>
&gt; newtype PKlass = PKlass (Ptr PKlass)<br>
&gt;<br>
&gt; foreign import ccall unsafe &quot;Klass_Create&quot;<br>
&gt;   kCreate :: CInt -&gt; CInt -&gt; IO PKlass<br>
&gt;<br>
&gt; foreign import ccall unsafe &quot;Klass_Destroy&quot;<br>
&gt;   kDestroy :: PKlass -&gt; IO ()<br>
&gt;<br>
&gt; foreign import ccall unsafe &quot;Klass_GetX&quot;<br>
&gt;   kGetX :: PKlass -&gt; IO CInt<br>
&gt; foreign import ccall unsafe &quot;Klass_GetY&quot;<br>
&gt;   kGetY :: PKlass -&gt; IO CInt<br>
&gt;<br>
&gt; foreign import ccall unsafe &quot;Klass_AddKlass&quot;<br>
&gt;   kAdd :: PKlass -&gt; PKlass -&gt; IO PKlass<br>
&gt;<br>
&gt;<br>
&gt; main = do<br>
&gt;   k &lt;- kCreate 4 5<br>
&gt;   kDestroy k<br>
&gt;   kGetY k &gt;&gt;= print   -- This shouldn&#39;t work<br>
&gt;   k&#39; &lt;- kCreate 2 8<br>
&gt;   k&#39;&#39; &lt;- k `kAdd` k&#39;<br>
&gt;   kDestroy k&#39;&#39;<br>
&gt;   kGetY k&#39;&#39; &gt;&gt;= print   -- This neither<br>
&gt;<br>
&gt;<br>
&gt; So it is very basic, and I can&#39;t understand why the supposedly destroyed objects are still here.<br>
&gt; Enclosed is all the source code (C++ class, C exportation and the Haskell main hereabove).<br>
&gt;<br>
&gt; I compile it this way:<br>
&gt; ghc --make main.hs *.cpp -lstdc++<br>
</div></div>&gt; &lt;test.zip&gt;_______________________________________________<br>
&gt; Haskell-Cafe mailing list<br>
&gt; <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br>
</blockquote></div><br>