using foreign-free on Win7 64-bit

Stephan Frank defclass at googlemail.com
Wed Sep 25 13:02:41 UTC 2013


I'm not sure if this is the real problem but aren't you mixing C++
allocation via new with C-deallocation via free and should instead also use
the C++ delete operator to free the memory, i.e. define a function such as

void deleteForMe(int* pointer)
{
     delete[] pointer;
}

and call that instead of a simple free?

Regs,

Stephan




2013/9/25 Rujia Liu <rujia.liu at gmail.com>

> Dear all,
>
> I have trouble using foreign-free on win7 64bit.
>
> My code:
>
> C++:
>
> int* gen(int n) {
>     int* x = new int[n];
>     for(int i = 0; i < n; i++) x[i] = i;
>     return x;
> }
>
> LISP:
>
> (defun gen-values (n)
>     (let* ((ret (cffi:foreign-funcall "gen" :int n :pointer)))
>             (dotimes (i n)
>                 (format t "~A~%" (mem-ref (mem-aptr ret :int i) :int)))
>             (foreign-free ret)))
>
> It works well on windows XP (32bit), but crashes on (foreign-free ret).
> SBCL win32 just crashes the process, while CCL win32 dropped me into kernel
> debugger. The topmost frame is a call to FREE.
>
> Our customers use both 32-bit and 64-bit systems, so I'd like to provide a
> single exe that runs on both 32-bit and 64-bits. Our dev machine is running
> a 32-bit Windows XP, uses VS2008 to compile to C++ library to DLL.
>
> After I commented out (foreign-free), the exe runs perfectly on both
> 32-bit and 64-bit, but there are memory leaks. Am I doing anything wrong?
>
> Thanks in advance.
>
> - Rujia
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20130925/cc8c74b0/attachment.html>


More information about the cffi-devel mailing list