Guile Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
FAQ FOOD! Re: scm_must_malloc and memory leaking?
forcer <forcer@mindless.com> writes:
> Hmm...
> When using
>
> f = scm_must_malloc(sizeof(some_struct));
> f->data = scm_must_malloc(size);
>
> and there won't be enough free memory for the second
> scm_must_malloc even after gc, the allocated memory in f is
> lost and won't be free'd. Is that true? and if yes, how can i
> prevent it?
Yes, I think there is a leak. Maybe you can use `malloc objects'. I
once made a wrapper for them, this might show you how to use them.
This is old code, so before it goes into the FAQ, it should be
checked.
#include <guile/gh.h>
#include <libguile.h>
/* Allocate some memory and stuff a magic Scheme value into *HANDLE so
that the memory gets freed when *HANDLE gets collected. Typically,
HANDLE points to some variable on the C stack. When this variable
goes out of scope, the memory gets freed. It might help the
garbage collector when you overwrite this variable after the memory
is no longer needed. */
void *gh_malloc_collected SCM_P ((size_t n, SCM *handle));
void *
gh_malloc_collected (n, handle)
size_t n;
SCM *handle;
{
*handle = scm_malloc_obj (n);
if (*handle == SCM_BOOL_F)
scm_memory_error ("gh_malloc_collected");
return SCM_MALLOCDATA(*handle);
}
void gh_detach_memory SCM_P ((SCM handle));
void
gh_detach_memory (handle)
SCM handle;
{
SCM_SETMALLOCDATA(handle, NULL);
}
Guile Home |
Main Index |
Thread Index