Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-06

How to free memory allocated by C++ library

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Malcolm Poole

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;
var
   conf: PInteger;
   FTesseract: TTesseract;
begin
   ...
      conf := tesseract_AllWordConfidences(FTesseract);
   ...
end;

====

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

/**
* Returns all word confidences (between 0 and 100) in an array, terminated
* 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();

}

Jorge Aldo G. de F. Junior
to Malcolm Poole

generally, the library itself exports a symbol capable of freeing allocated memory...

can you find any reference to a delete function in this lib ?

2011/6/1 Malcolm Poole :

Show quoted text 50 lines
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; var  conf: PInteger;  FTesseract: TTesseract; begin  ...     conf := tesseract_AllWordConfidences(FTesseract);  ... end; ==== C function, and documentation from C++ baseapi.h  /**   * Returns all word confidences (between 0 and 100) in an array, terminated   * 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(); } _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Henry Vermaak
to Malcolm Poole: Working from the instructions in CinFreepascal.pdf (…
Malcolm Poole wrote 12 lines
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.

If you add cmem to the start of your uses list, all the memory operations are routed through the c memory manager. You should then be able to use Free safely for pointers that were allocated by your library.

I'm planning some ocr work in the near future, so let us know if this works!

Henry

Jonas Maebe
to Henry Vermaak
Henry Vermaak wrote 15 lines
On 01/06/11 21:27, Malcolm Poole wrote: > 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. If you add cmem to the start of your uses list, all the memory operations are routed through the c memory manager. You should then be able to use Free safely for pointers that were allocated by your library.

That is incorrect. The cmem unit adds extra size information to all allocated memory blocks (for so that memsize() works). Additionally, most C++ memory allocations happen via new/delete, and using libc's "free" to free a memory block allocated by "new" is almost guaranteed to cause crashes down the line.

You'd have to write a separate cppmem unit that exports the various kinds of new/delete helpers. To make things extra fun, their mangled names depend on the used C++ compiler and sometimes even version (and also which helpers exist can change, e.g., I believe older g++ versions had a single helper for delete and delete[], which the current versions have two different ones for that).

Jonas

Henry Vermaak
to Jonas Maebe
Jonas Maebe wrote 6 lines
On 02 Jun 2011, at 10:35, Henry Vermaak wrote: > > If you add cmem to the start of your uses list, all the memory operations are routed through the c memory manager. You should then be able to use Free safely for pointers that were allocated by your library. That is incorrect. The cmem unit adds extra size information to all allocated memory blocks (for so that memsize() works). Additionally, most C++ memory allocations happen via new/delete, and using libc's "free" to free a memory block allocated by "new" is almost guaranteed to cause crashes down the line.

So it works in C, but not C++? Or have I completely misunderstood the documentation?

You'd have to write a separate cppmem unit that exports the various kinds of new/delete helpers. To make things extra fun, their mangled names depend on the used C++ compiler and sometimes even version (and also which helpers exist can change, e.g., I believe older g++ versions had a single helper for delete and delete[], which the current versions have two different ones for that).

Or write a function for the library that takes care of disposing pointers.

Henry

Jonas Maebe
to Henry Vermaak
Henry Vermaak wrote 9 lines
On 02/06/11 13:04, Jonas Maebe wrote: > > On 02 Jun 2011, at 10:35, Henry Vermaak wrote: >> >> If you add cmem to the start of your uses list, all the memory operations are routed through the c memory manager. You should then be able to use Free safely for pointers that were allocated by your library. > > That is incorrect. The cmem unit adds extra size information to all allocated memory blocks (for so that memsize() works). Additionally, most C++ memory allocations happen via new/delete, and using libc's "free" to free a memory block allocated by "new" is almost guaranteed to cause crashes down the line. So it works in C, but not C++? Or have I completely misunderstood the documentation?

You will not be able to use it to free memory allocated in either C or C++ programs, unless all they do is use malloc *and* after every allocation increase the pointer with sizeof(ptruint). And even that's just an implementation detail on which you cannot rely.

The main use of cmem is to offer the same functionality as the sharemem unit.

Jonas

Malcolm Poole
to Henry Vermaak: So it works in C, but not C++? Or have I completely misunderstood the documentation? Or…
Henry Vermaak wrote 28 lines
On 02/06/11 13:04, Jonas Maebe wrote: > > On 02 Jun 2011, at 10:35, Henry Vermaak wrote: >> >> If you add cmem to the start of your uses list, all the memory >> operations are routed through the c memory manager. You should then >> be able to use Free safely for pointers that were allocated by your >> library. > > That is incorrect. The cmem unit adds extra size information to all > allocated memory blocks (for so that memsize() works). Additionally, > most C++ memory allocations happen via new/delete, and using libc's > "free" to free a memory block allocated by "new" is almost guaranteed > to cause crashes down the line. So it works in C, but not C++? Or have I completely misunderstood the documentation? > You'd have to write a separate cppmem unit that exports the various > kinds of new/delete helpers. To make things extra fun, their mangled > names depend on the used C++ compiler and sometimes even version (and > also which helpers exist can change, e.g., I believe older g++ > versions had a single helper for delete and delete[], which the > current versions have two different ones for that). Or write a function for the library that takes care of disposing pointers.

Yes, that was the answer I reached an hour after posting. It's surprising how writing to a mailing list can concentrate the mind :)

BTW, the library works very well. It interfaces with libtesseract v.3. It and bindings for many related leptonica image processing functions are at
http://code.google.com/p/ocrivist/source/browse/

Malcolm