Discussion:
Freeing Enumerating Memory
(too old to reply)
a***@aol.com
2010-06-04 06:49:03 UTC
Permalink
When I am enumerating items returned by an API call I use ...

with SomeEnumAPIStruct do
. . .
cbItem := <N>;
ptrEnumMemory := AllocMem(<N> * SizeOf(TEnumItem));
ptr4FreeMem := ptrEnumMemory; // for freeing because PtrEnumMemory
gets incremented
. . .
end;

// get the enumerated items into the memory block

CallEnumerate(@SomeEnumApiStruct);

This returns the <N> items in the enumeration memory.

Then I enumerate the items using . . .

for i := 0 to <N - 1> do
SomeValue := TEnumItem(PtrEnumMemory^).Element;
inc(PtrEnumMemory);
end;

Then I . . .

FreeMem(ptr4FreeMem);

Now my question, does _all_ the memory block get freed, or only one
portion equivalent to SizeOf(TEnumItem).

Alan Lloyd
Maarten Wiltink
2010-06-04 07:21:52 UTC
Permalink
Post by a***@aol.com
When I am enumerating items returned by an API call I use ...
with SomeEnumAPIStruct do
. . .
cbItem := <N>;
ptrEnumMemory := AllocMem(<N> * SizeOf(TEnumItem));
ptr4FreeMem := ptrEnumMemory; // for freeing because PtrEnumMemory
gets incremented
. . .
end;
// get the enumerated items into the memory block
This returns the <N> items in the enumeration memory.
Then I enumerate the items using . . .
for i := 0 to <N - 1> do
SomeValue := TEnumItem(PtrEnumMemory^).Element;
inc(PtrEnumMemory);
end;
Then I . . .
FreeMem(ptr4FreeMem);
Now my question, does _all_ the memory block get freed, or only one
portion equivalent to SizeOf(TEnumItem).
I'd say that the whole enumerating business is a red herring.
One AllocMem, one FreeMem.

Groetjes,
Maarten Wiltink
a***@aol.com
2010-06-04 10:46:25 UTC
Permalink
Post by Maarten Wiltink
I'd say that the whole enumerating business is a red herring.
One AllocMem, one FreeMem.
In other words the FreeMem knows what and how much to release from the
value returned by the AllocMem (and later passed to FreeMem. And so I
do need to store that value (to use at FreeMem time) if I am going to
increment the original returned value.

However the Inc() increments by the SizeOf() the type of the pointer
to which I assign the AllocMem() value. And that is all Delphi does.

Alan Lloyd
BRoberts
2010-06-04 14:54:30 UTC
Permalink
Post by a***@aol.com
Post by Maarten Wiltink
I'd say that the whole enumerating business is a red herring.
One AllocMem, one FreeMem.
In other words the FreeMem knows what and how much to release from the
value returned by the AllocMem (and later passed to FreeMem. And so I
do need to store that value (to use at FreeMem time) if I am going to
increment the original returned value.
However the Inc() increments by the SizeOf() the type of the pointer
to which I assign the AllocMem() value. And that is all Delphi does.
Alan Lloyd
A 4 byte length descriptor prefixes any memory block returned by AllocMem.
Presuming you pass FreeMem the value returned by AllocMem the entire block
will be released with the one call. (See the Memory Management section of
the Object Pascal Reference Manual.) If you need to increment the pointer
then do it on a copy of the original return value.

Loading...