Problem solved - A buffer length was not properly initialized.

On Mon, Feb 9, 2015 at 3:50 PM, Mirko Vukovic <mirko.vukovic@gmail.com> wrote:
Hello,

I am running 64-bit CCL 1.10 on Windows 7.  I'm writing an interface to a National Instruments library (dll)
The library was downloaded from the NI site.  It was compiled using Microsofts C++ compiler.

I get a stack dump when a pointer is being freed using foreign-free.  This error occurs not in a fresh lisp
session, but after a second or third call to the same function (with same arguments).

One more detail: the library is used to access data in files in NI's TDMS format.  This error does not
seem to happen on all files that I am trying to access, only some of them.

I don't have much experience in cross-language library interface building, and I am wondering if I am making
some kind of mistake.  I am hoping for pointers on how to debug this further.

I define a pointer of type :uint to access results of a call to a library function that places them into an
unsigned int variable.  I then retrieve the value using using mem-ref, and the free the pointer. 
This is where the stack dump happens.

What follows is the defcfun, and the lisp function.  I mark with *** the pointer, and the relevant variable
definitions that pertain to it:

The C-function interface (with the C-documentation included) is
(defcfun (ddc-get-num-channel-group-properties "DDC_GetNumChannelGroupProperties")
    ddc-code
  "
This function will get numberOfProperites (via a pointer)

int DDC_GetNumChannelGroupProperties (DDCChannelGroupHandle channelGroup,
                                      unsigned int *numberOfProperties); *******"
  (group-handle group-handle) ;; group-handle is define to be a pointer type
  (num-channel-group-properties :pointer)) ;;*******

The pointer issue arises with the second variable num-channel-group-properties.

The lisp code where the error occurs is below (relevant lines highlighted with ***).

(defun get-num-channel-group-properties (group-handle*)
  "Get number of file properties

FILE-HANDLE* is a pointer to the file handle"
;;  (trace foreign-alloc)
  (let ((number-of-properties
       (let ((number-of-properties* (foreign-alloc :uint))) ;;******
         (print 'allocated-pointer)
         (print number-of-properties*)
         (ddc-get-num-channel-group-properties group-handle*
                           number-of-properties*)
         (print 'done-with-ddc)
         (prog2
         (print 'acessing-number-of-properties)
         (mem-ref number-of-properties* :uint) ;; ********
           (print 'freeing-pointer)
           (foreign-free number-of-properties*) ;; ********
           (print 'freed-pointer)))))
  ;;  (untrace foreign-alloc)
    (print 'ready-to-return-with-value)
    number-of-properties))

The traceback is:

ALLOCATED-POINTER
#<A Foreign Pointer #x5FFC70>
DONE-WITH-DDC
ACESSING-NUMBER-OF-PROPERTIES
FREEING-POINTER
 1> Calling (FOREIGN-FREE #<A Foreign Pointer #x5FFC70>)

and the debugger traceback is

(#x0000000026479810) #x00000000000F532C : #<Function FREE #x00000001000F51BF> + 365
(#x0000000026479840) #x0000000001491314 : #<Function (TRACED FOREIGN-FREE) #x000000210149109F> + 629
(#x0000000026479888) #x000000000139DE14 : #<Function GET-NUM-CHANNEL-GROUP-PROPERTIES #x000000210139DCDF> + 309

So, to summarize:

I have created a pointer to access results of a library call.  Library accesses contents of files created with National
Instruments software.  Library is compiled with MS C++.

The pointer free crash occurs on the third call of the lisp function.  The first two calls (with same arguments)
run OK.

Thanks,

Mirko