Board index » delphi » Allocating global memory - why doesn't this work?

Allocating global memory - why doesn't this work?

I'm using a DLL (written in C I think) in a delphi program.  For most of the
functions in the DLL I need to pass a pointer to an array of RealTypes.

The example programs that come with the DLL do this in the following way:

type
    LPREAL = ^Realtype;

var
    lpY1 : LPREAL;

begin
    yHnd := GlobalAlloc (GHND, sizeof(realtype) * NoOfDataPoints);
    lpY1 := GlobalLock (yHnd);

lpY1 is then passed as a parameter to the DLL procedure.

I'd prefer to use Delphi memory management functions to take advantage of
exception handling etc. so I replaced the API calls like this:

    GetMem(lpY1, sizeof(realType) * NoOfDataPoints);

This causes a GPF in the DLL.  Can anyone explain to me why this doesn't
work?  

Thanks,
 -David
s934...@minyos.xx.rmit.edu.au

 

Re:Allocating global memory - why doesn't this work?


In article <4aah79$...@aggedor.rmit.EDU.AU>
           s934...@minyos.xx.rmit.EDU.AU "David John Chapman" writes:

Quote

> I'm using a DLL (written in C I think) in a delphi program.  For most of the
> functions in the DLL I need to pass a pointer to an array of RealTypes.

> The example programs that come with the DLL do this in the following way:

> type
>     LPREAL = ^Realtype;

> var
>     lpY1 : LPREAL;

> begin
>     yHnd := GlobalAlloc (GHND, sizeof(realtype) * NoOfDataPoints);
>     lpY1 := GlobalLock (yHnd);

> lpY1 is then passed as a parameter to the DLL procedure.

> I'd prefer to use Delphi memory management functions to take advantage of
> exception handling etc. so I replaced the API calls like this:

>     GetMem(lpY1, sizeof(realType) * NoOfDataPoints);

> This causes a GPF in the DLL.  Can anyone explain to me why this doesn't
> work?  

> Thanks,
>  -David
> s934...@minyos.xx.rmit.edu.au

One possibility is that sizeof(realType) * NoOfDataPoints is > 64K - you
can't allocate things this large with GetMem, but you can with GlobalAlloc.
If you don't have overflow checking on, the result of the multiplication will
wrap around and you may not be allocating anything like as much memory as you
think you are!

Hope this helps,

Catherine.
--
Catherine Rees-Lay                 Cathe...@polyhdrn.demon.co.uk

Polyhedron Software Ltd.        
Programs for Programmers - QA, Compilers, Graphics

************ Visit our Web site on http://www.polyhedron.co.uk/ ************

Other Threads