Forum > General

Using C object files with Pascal with heaptrc

(1/2) > >>

LongTimePascaler:
I have been using heaptrc to locate memory leaks as I develop a fairly large suite of software.  I now need to link to some software written in C (compiled with gcc); the C is fairly old, tightly written and riddled with dynamic memory allocation.  Normally I would just translate the C to Pascal to keep everything simple, but reading through this C code I decided that life was too short ...
For convenience I have written a unit as a wrapper, and this unit LINKLIBs to the 'c' and 'm' C libraries and LINKs to the C object file.  In test programs this works well without any use of CMem.
What is passed to the C code is a Pascal record that has a number of dynamically allocated arrays as fields, and a couple of similar records which the C code will use by dynamically allocating (malloc) memory as the fields' arrays and which are then passed back to the Pascal code to use.  The C links happily with the Pascal and for simple cases runs fine, but it doesn't take much complexity before the whole thing collapses at random points with memory errors. For example:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Marked memory at $00007FD0F03BFF30 invalidWrong signature $1F4AE121 instead of 5998D445 I assume (given the warning on the Wiki page https://wiki.freepascal.org/heaptrc) that there is a clash between heaptrc (as memory manager) and the C memory manager used by the C code.  I also assume that heaptrc is incompatible with the unit CMem.
Are my assumptions correct?  Is there any way for heaptrc and C code to cohabit, or are they completely incompatible so that it is either use heaptrc or link to C code (but not both)?

MarkMLl:

--- Quote from: LongTimePascaler on July 01, 2022, 06:52:35 pm ---I assume (given the warning on the Wiki page https://wiki.freepascal.org/heaptrc) that there is a clash between heaptrc (as memory manager) and the C memory manager used by the C code.  I also assume that heaptrc is incompatible with the unit CMem.
Are my assumptions correct?  Is there any way for heaptrc and C code to cohabit, or are they completely incompatible so that it is either use heaptrc or link to C code (but not both)?

--- End quote ---

I'm working from memory here since I'm without my principal development system (destructive mains glitch). I think there's ways... see https://forum.lazarus.freepascal.org/index.php?topic=46121.0 as an example (via Google, forum search might turn up others).

MarkMLl

marcov:
I think so. CMem doesn't replace the C memory allocation routines, so all allocations made by C (and possibly also FPC allocations deallocated by C) are not tracked.

You might want to look at valgrind.

PascalDragon:

--- Quote from: LongTimePascaler on July 01, 2022, 06:52:35 pm ---What is passed to the C code is a Pascal record that has a number of dynamically allocated arrays as fields, and a couple of similar records which the C code will use by dynamically allocating (malloc) memory as the fields' arrays and which are then passed back to the Pascal code to use.
--- End quote ---

In addition to what marcov wrote: I hope you're not using Pascal dynamic arrays here.

LongTimePascaler:
@MarkMLl My searching didn't come up with that reference; perhaps my search words weren't well enough chosen. I will study that thread.

@marcov I have used valgrind in the past to isolate bottlenecks, but not for finding memory leaks.  I will have to investigate that.

@PascalDragon Yes, The Pascal record mimics the C struct which uses dynamically allocated arrays.  The C code sometimes traverses its arrays using indices and sometimes using pointer arithmetic.  None of the arrays allocated in the Pascal code is freed by the C code, they are just read/traversed and that seems to work. 
The structs/records returned (through the parameters) by the C code do have array fields that are dynamically allocated by the C code.  These are treated as dynamically allocated arrays by the Pascal code.  The Pascal code takes the data out of those arrays and puts them into the Pascal program's data structures.  Then another C function is called to free those C-allocated dynamic arrays; that function simply uses C's free function to dealloacate those arrays.  If I do this just once this works, but if the Pascal code happens to do it more than once the program crashes with the kind of memory error given above.  Probably (I haven't checked) if I didn't try to free that memory the software wouldn't crash, but then I would have a persistent memory leak.
There are some other complications involved in getting anything to work, such as setting up the arguments to the principal C function (the one that does the work) in the Pascal calling code, but since I have found a way to make that work so that the C stuff runs and returns sensible values I am concentrating on the memory management issue here.

I have other things I must do today but I'll return to this tomorrow and follow up what @MarkMLl and @marcov have suggested.  @PascalDragon if there is an alternative to using dynamically allocated arrays then please advise.

Navigation

[0] Message Index

[#] Next page

Go to full version