Richard R wrote:
> Hello, in my project, a variable needs to be allocated since its
> outside the scope of where it will be used. The variable is a pointer
> type inside a record structure. I initialize this pointer with
> GetMem(variable, sizeof(pointer))

You now have a pointer to a pointer. Is that what you intended?

I suggest you declare "variable" to be a specific pointer type, and then
allocate it with New instead of GetMem. That way, it's impossible to
get the size wrong; New knows what kind of thing you're allocating. At 
the very least, use SizeOf(variable^) instead of SizeOf(Pointer).

> However, when it's passed into a function, a new pointer is assigned
> to it. If I call FreeMem it raises a Invalid pointer operation
> exception. So What I'm wondering, is there a memory leak using GetMem
> if I dont use FreeMem since the variable get reassigned?

Yes. GetMem works like this:

variable := AllocMem(Size);

That is, it assigns a new value into the variable. If you later assign
another value into that variable, then the GetMem-assigned value is
lost. (AllocMem is a little different in that it sets the allocated
memory's contents to all-bits-zero, but the "assignment" concept is
easier to illustrate with AllocMem.)

Catch possible errors in pointer assignments by turning on the "typed @
operator" compiler option. Also, make sure you never have anything
declared as just a Pointer. If you're pointing to an Integer, use a
PInteger. If you're pointing to a TObject, then declare a new type
PObject = ^TObject and use that.

And finally, see whether you can configure your e-mail program to wrap 
long lines before sending.

-- 
Rob
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to