Forum > General

Using C object files with Pascal with heaptrc

<< < (2/2)

440bx:

--- 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 ---
Mixing different compiler's memory managers is always problematic.  As the saying goes: too many cooks in the memory management kitchen.

If I were you, I'd change both, the Pascal code and the C code to directly use the O/S's memory management services.  This way, there is only _one_ memory manager involved and that makes life a lot easier.

You didn't mention what O/S you're targeting but, if it's Windows only, Windows offers a good number of very convenient methods to solve that problem.

HTH.

MarkMLl:

--- Quote from: 440bx on July 04, 2022, 10:29:41 am ---If I were you, I'd change both, the Pascal code and the C code to directly use the O/S's memory management services.  This way, there is only _one_ memory manager involved and that makes life a lot easier.

--- End quote ---

I'm not sure that is viable if e.g. Pascal strings (i.e. on the heap, as distinct from short strings) are being used.

MarkMLl

440bx:

--- Quote from: MarkMLl on July 04, 2022, 10:50:05 am ---I'm not sure that is viable if e.g. Pascal strings (i.e. on the heap, as distinct from short strings) are being used.

--- End quote ---
Only the OP knows if that's a viable alternative.  Depends on the code.  It's a possibility/option he may want to look into.

PascalDragon:

--- Quote from: LongTimePascaler on July 04, 2022, 10:00:55 am ---@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.

--- End quote ---

Because your message doesn't clearly state this: do you or do you not use array of XXX fields in the record/struct that is shared between the C and the Pascal code? If you do so: DON'T. A Pascal dynamic array is a much different beast than what passes as an array in C. If you want to have your C and Pascal code communicate you need to use the lowest common denominator, which in this case is C, and thus you need to use pointer variables and manually allocate/free them like you do in C to represent arrays.

Navigation

[0] Message Index

[*] Previous page

Go to full version