Re: REPOST: Memory-allocation rules with Delphi .exe, C/C++ .dll?

From: Elias Sabbagh (ehs_at_sabbagh.com)
Date: 07/31/04


Date: 30 Jul 2004 16:30:55 -0700

Catherine Rees Lay <spamtrap@polyhedron.org.uk> wrote in message news:<TiUMTEFzLhCBFwe8@spamtram.polyhedron.com>...
> In article <b57fb7.0407291310.6b30568b@posting.google.com>, Elias
> Sabbagh <ehs@sabbagh.com> writes
> >Hi everyone--
> >
> >(reposting with what I hope is a clearer question...)
> >
> >Are either of these two scenarios forbidden?
> >
> >Scenario 1: Delphi .exe allocates mem (either SetLength() on dynamic
> >array, or GetMem() on pointer). Passes it to routine found in a C/C++
> >.dll (linked to using the cdecl calling convention). C/C++ .dll
> >routine presumably stores pointer in a struct, occasionaly modifying
> >values stored in the memory that the pointer points to. Later, Delphi
> >.exe deallocates the mem (Finalize() or FreeMem()).
> >
> >Scenario 2: The opposite case -- C/C++ .dll provides specialty
> >allocation routines, aglMalloc(), and aglFree(). Delphi .exe calls
> >aglMalloc(), and keeps track of the Pointer that it returns. Delphi
> >.exe modifies the memory through the use of a PSingle, an alias of
> >Pointer. Later, Delphi calls aglFree() on the original Pointer.
> >
> >Thanks in advance,
> >
> >Elias Sabbagh
>
> The only thing that would worry me is that the library bothered to
> provide specialised allocation routines. Why? Is it possible that
> something extra is being allocated and deallocated internally which
> isn't visible to the caller? If so, scenario 1 will probably not work,
> but scenario 2 should be OK.
>
> Catherine.

Catherine-

It's Scenario 1A taking place in my code, actually, and I also think
that the situation that had you worried must be happening, as I can't
seem to get the following code to work:

type
  array of integer = TDynamicArrayOfInteger;

{ This routine returns the name of the memory-mapped file that an
ActiveX plotting control will plot. It needs to be told the rank,
size, and shape of the array that's being plotted. }
function ShareDataWithPlotter(num_bytes: integer; dimsizes:
TDynamicArrayofInteger): string;
var
  mem: Pointer;
begin
  ...
  mem := aglMalloc(num_bytes); // This routine comes from the C/C++
.dll
  if( mem <> nil ) then
  begin
    ... // Fill the array with data here
    if( aglReshape(mem, Length(dimsizes), dimsizes[0], ...) = SUCCESS
) then
      Result := aglGetShareName(mem) // THIS LINE IS NEVER REACHED.
DAMMIT.
    else
      aglFree(mem); // This routine comes from the C/C++ .dll
  end;
end;

I've already checked to see that dimsizes is actually allocated before
it's used. My guess is that aglReshape() is doing something horrible
to the dimsizes array, like combining it with mem in some screwy way.
The original C prototype starts like this:

int aglReshape(pvoid ary, int numdims, int *dimsizes, ...);

and my translation looks like this:

function aglReshape(ary: Pointer; numdims: integer; var dimsizes:
integer; ...): integer; cdecl; external 'aview160.dll';

To sum up: mem is allocated on the C/C++ .dll side, dimsizes is
allocated on the Delphi .exe side, and both get passed to a routine on
the C/C++ side.

<$64,000> What could be causing aglReshape() to fail? </$64,000>

Elias Sabbagh



Relevant Pages

  • Re: interop memory allocation
    ... pointer for the C function to be reliable when the memory is allocated ... in managed code? ... about this particular scenario? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pointers in derived types, help needed
    ... subroutine/function must have an explicit interface. ... C is not a pointer. ... but what eluded me was the fact that encapsulating a routine ... mean that pointers in CC point to the same memory as pointers in DD? ...
    (comp.lang.fortran)
  • testing a pointer
    ... I vaguely remember a library call to test a pointer to see if the ... memory address is readable and/or writeable. ... I further recall that ... I can't seem to find the routine. ...
    (comp.os.vms)
  • Re: DMA operation to 64-bits PC platform
    ... - the application allocates the memory. ... a pointer to this memory is passed to the driver (custom made ... Drivers obtain the address of this routine by calling ...
    (comp.arch.fpga)
  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)