Re: REPOST: Memory-allocation rules with Delphi .exe, C/C++ .dll?
From: Elias Sabbagh (ehs_at_sabbagh.com)
Date: 07/31/04
- Next message: Bjørge Sæther: "Re: MonthCalendar with or without week-ends and week-ends being bold"
- Previous message: Martin Harvey (Demon account): "Re: MonthCalendar with or without week-ends and week-ends being bold"
- In reply to: Catherine Rees Lay: "Re: REPOST: Memory-allocation rules with Delphi .exe, C/C++ .dll?"
- Next in thread: Martin Harvey (Demon account): "Re: REPOST: Memory-allocation rules with Delphi .exe, C/C++ .dll?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Bjørge Sæther: "Re: MonthCalendar with or without week-ends and week-ends being bold"
- Previous message: Martin Harvey (Demon account): "Re: MonthCalendar with or without week-ends and week-ends being bold"
- In reply to: Catherine Rees Lay: "Re: REPOST: Memory-allocation rules with Delphi .exe, C/C++ .dll?"
- Next in thread: Martin Harvey (Demon account): "Re: REPOST: Memory-allocation rules with Delphi .exe, C/C++ .dll?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|