<div dir="ltr">Problem solved - A buffer length was not properly initialized.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 9, 2015 at 3:50 PM, Mirko Vukovic <span dir="ltr"><<a href="mailto:mirko.vukovic@gmail.com" target="_blank">mirko.vukovic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>Hello,<br><br></div>I am running 64-bit CCL 1.10 on Windows 7. I'm writing an interface to a National Instruments library (dll)<br></div><div>The library was downloaded from the NI site. It was compiled using Microsofts C++ compiler.<br></div><div><br></div>I get a stack dump when a pointer is being freed using foreign-free. This error occurs not in a fresh lisp <br>session, but after a second or third call to the same function (with same arguments).<br><br></div><div>One more detail: the library is used to access data in files in NI's TDMS format. This error does not<br>seem to happen on all files that I am trying to access, only some of them.<br><br></div><div>I don't have much experience in cross-language library interface building, and I am wondering if I am making<br>some kind of mistake. I am hoping for pointers on how to debug this further.<br></div><div><br></div><div>I define a pointer of type :uint to access results of a call to a library function that places them into an<br>unsigned int variable. I then retrieve the value using using mem-ref, and the free the pointer. <br>This is where the stack dump happens.<br></div><div><br></div><div>What follows is the defcfun, and the lisp function. I mark with *** the pointer, and the relevant variable<br>definitions that pertain to it:<br></div><div><br></div>The C-function interface (with the C-documentation included) is<br><span style="font-family:monospace,monospace">(defcfun (ddc-get-num-channel-group-properties "DDC_GetNumChannelGroupProperties")<br> ddc-code<br> "<br>This function will get numberOfProperites (via a pointer)<br><br>int DDC_GetNumChannelGroupProperties (DDCChannelGroupHandle channelGroup, <br> unsigned int *numberOfProperties); *******"<br> (group-handle group-handle) ;; group-handle is define to be a pointer type<br> (num-channel-group-properties :pointer)) ;;*******<br></span><br></div>The pointer issue arises with the second variable <span style="font-family:monospace,monospace">num-channel-group-properties.<br><br></span></div><div><span style="font-family:arial,helvetica,sans-serif">The lisp code where the error occurs is below (relevant lines highlighted with ***).</span><span style="font-family:monospace,monospace"><br><br>(defun get-num-channel-group-properties (group-handle*)<br> "Get number of file properties<br><br>FILE-HANDLE* is a pointer to the file handle"<br>;; (trace foreign-alloc)<br> (let ((number-of-properties<br> (let ((number-of-properties* (foreign-alloc :uint))) ;;******<br> (print 'allocated-pointer)<br> (print number-of-properties*)<br> (ddc-get-num-channel-group-properties group-handle*<br> number-of-properties*)<br> (print 'done-with-ddc)<br> (prog2<br> (print 'acessing-number-of-properties)<br> (mem-ref number-of-properties* :uint) ;; ********<br> (print 'freeing-pointer)<br> (foreign-free number-of-properties*) ;; ********<br> (print 'freed-pointer)))))<br> ;; (untrace foreign-alloc)<br> (print 'ready-to-return-with-value)<br> number-of-properties))<br><br></span></div><div><span style="font-family:arial,helvetica,sans-serif">The traceback is:</span><span style="font-family:monospace,monospace"><br><br>ALLOCATED-POINTER <br>#<A Foreign Pointer #x5FFC70> <br>DONE-WITH-DDC <br>ACESSING-NUMBER-OF-PROPERTIES <br>FREEING-POINTER <br> 1> Calling (FOREIGN-FREE #<A Foreign Pointer #x5FFC70>) <br><br></span></div><div><span style="font-family:arial,helvetica,sans-serif">and the debugger traceback is <br></span><span style="font-family:monospace,monospace"><br>(#x0000000026479810) #x00000000000F532C : #<Function FREE #x00000001000F51BF> + 365<br>(#x0000000026479840) #x0000000001491314 : #<Function (TRACED FOREIGN-FREE) #x000000210149109F> + 629<br>(#x0000000026479888) #x000000000139DE14 : #<Function GET-NUM-CHANNEL-GROUP-PROPERTIES #x000000210139DCDF> + 309<br><br></span></div><div><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">So, to summarize:<br><br></font></span></div><div><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">I have created a pointer to access results of a library call. Library accesses contents of files created with National<br>Instruments software. Library is compiled with MS C++.<br></font></span></div><div><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif"><br>The pointer free crash occurs on the third call of the lisp function. The first two calls (with same arguments)<br></font></span></div><div><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">run OK.<br><br></font></span></div><div><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">Thanks,<br><br>Mirko<br></font></span></div><span style="font-family:monospace,monospace"></span></div>
</blockquote></div><br></div>