[fpc-pascal] How to free memory allocated by C++ library

Malcolm Poole malcolm at lingua-z.co.uk
Wed Jun 1 22:27:32 CEST 2011

Working from the instructions in CinFreepascal.pdf ( 
ftp://ftp.freepascal.org/pub/fpc/docs-pdf/CinFreePascal.pdf ) I have 
written an interface library to the C++ library libtesseract ( 
http://code.google.com/p/tesseract-ocr ).

Everything appears to work well but I am unsure about how to free memory 
which has been reserved by a libtesseract function and returned as a 
result. The original documentation states that "The calling function 
must delete [] after use", but the application crashes with an Access 
Violation if I try to free the memory with Dispose or Freemem. I can 
confirm that the array of Integer pointed to by the result of 
tesseract_AllWordConfidences contains valid data.

I would be grateful for any help.

- Malcolm


Freepascal declaration and code snippet:
function tesseract_AllWordConfidences( APIHandle: TTesseract ): 
PInteger; stdcall; external LIBTESSINTF;

   conf: PInteger;
   FTesseract: TTesseract;
      conf := tesseract_AllWordConfidences(FTesseract);


C function, and documentation from C++ baseapi.h

    * Returns all word confidences (between 0 and 100) in an array, 
    * by -1.  The calling function must delete [] after use.
    * The number of confidences should correspond to the number of space-
    * delimited words in GetUTF8Text.
extern int* EXPORTCALL tesseract_AllWordConfidences(tessHandle APIHandle){
    return APIHandle->AllWordConfidences();

More information about the fpc-pascal mailing list