Commit 70148846 authored by Kai Zhang's avatar Kai Zhang

fix memory leak

parent 2183f77c
#include <igraph/igraph.h> #include <igraph/igraph.h>
#include "haskell_attributes.h" #include "haskell_attributes.h"
void my_igraph_vector_destroy(igraph_vector_t* v) {
printf("free vector\n");
igraph_vector_destroy(v);
}
const igraph_attribute_table_t igraph_haskell_attribute_table={ const igraph_attribute_table_t igraph_haskell_attribute_table={
&igraph_haskell_attribute_init, &igraph_haskell_attribute_destroy, &igraph_haskell_attribute_init, &igraph_haskell_attribute_destroy,
&igraph_haskell_attribute_copy, &igraph_haskell_attribute_add_vertices, &igraph_haskell_attribute_copy, &igraph_haskell_attribute_add_vertices,
......
...@@ -5,6 +5,4 @@ ...@@ -5,6 +5,4 @@
void haskelligraph_init(); void haskelligraph_init();
void my_igraph_vector_destroy(igraph_vector_t* v);
#endif #endif
...@@ -68,7 +68,7 @@ import Data.List.Split (chunksOf) ...@@ -68,7 +68,7 @@ import Data.List.Split (chunksOf)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
{#pointer *igraph_vector_t as Vector foreign finalizer {#pointer *igraph_vector_t as Vector foreign finalizer
my_igraph_vector_destroy newtype#} igraph_vector_destroy newtype#}
-- Construtors and destructors -- Construtors and destructors
...@@ -78,7 +78,7 @@ allocaVector f = mallocBytes {# sizeof igraph_vector_t #} >>= f ...@@ -78,7 +78,7 @@ allocaVector f = mallocBytes {# sizeof igraph_vector_t #} >>= f
addVectorFinalizer :: Ptr Vector -> IO Vector addVectorFinalizer :: Ptr Vector -> IO Vector
addVectorFinalizer ptr = do addVectorFinalizer ptr = do
vec <- newForeignPtr my_igraph_vector_destroy ptr vec <- newForeignPtr igraph_vector_destroy ptr
return $ Vector vec return $ Vector vec
{-# INLINE addVectorFinalizer #-} {-# INLINE addVectorFinalizer #-}
...@@ -128,9 +128,21 @@ toList vec = do ...@@ -128,9 +128,21 @@ toList vec = do
{#pointer *igraph_vector_ptr_t as VectorPtr foreign finalizer {#pointer *igraph_vector_ptr_t as VectorPtr foreign finalizer
igraph_vector_ptr_destroy_all newtype#} igraph_vector_ptr_destroy newtype#}
{#fun igraph_vector_ptr_init as igraphVectorPtrNew { +, `Int' } -> `VectorPtr' #} allocaVectorPtr :: (Ptr VectorPtr -> IO a) -> IO a
allocaVectorPtr f = mallocBytes {# sizeof igraph_vector_ptr_t #} >>= f
{-# INLINE allocaVectorPtr #-}
addVectorPtrFinalizer :: Ptr VectorPtr -> IO VectorPtr
addVectorPtrFinalizer ptr = do
vec <- newForeignPtr igraph_vector_ptr_destroy ptr
return $ VectorPtr vec
{-# INLINE addVectorPtrFinalizer #-}
{#fun igraph_vector_ptr_init as igraphVectorPtrNew
{ allocaVectorPtr- `VectorPtr' addVectorPtrFinalizer*
, `Int' } -> `CInt' void- #}
{#fun igraph_vector_ptr_e as ^ { `VectorPtr', `Int' } -> `Ptr ()' #} {#fun igraph_vector_ptr_e as ^ { `VectorPtr', `Int' } -> `Ptr ()' #}
{#fun igraph_vector_ptr_set as ^ { `VectorPtr', `Int', id `Ptr ()' } -> `()' #} {#fun igraph_vector_ptr_set as ^ { `VectorPtr', `Int', id `Ptr ()' } -> `()' #}
......
...@@ -21,11 +21,13 @@ import IGraph.Internal.C2HS ...@@ -21,11 +21,13 @@ import IGraph.Internal.C2HS
allocaIGraph :: (Ptr IGraph -> IO a) -> IO a allocaIGraph :: (Ptr IGraph -> IO a) -> IO a
allocaIGraph f = mallocBytes {# sizeof igraph_t #} >>= f allocaIGraph f = mallocBytes {# sizeof igraph_t #} >>= f
{-# INLINE allocaIGraph #-}
addIGraphFinalizer :: Ptr IGraph -> IO IGraph addIGraphFinalizer :: Ptr IGraph -> IO IGraph
addIGraphFinalizer ptr = do addIGraphFinalizer ptr = do
vec <- newForeignPtr igraph_destroy ptr vec <- newForeignPtr igraph_destroy ptr
return $ IGraph vec return $ IGraph vec
{-# INLINE addIGraphFinalizer #-}
{#fun igraph_empty as igraphNew' {#fun igraph_empty as igraphNew'
{ allocaIGraph- `IGraph' addIGraphFinalizer* { allocaIGraph- `IGraph' addIGraphFinalizer*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment