Thanks for the info. I think I came up with a solution that doesnt cause a memory leak. Since I'm dealing with the host application and conversion from vc++ headers, I have to deal with raw pointer types. Now as to initializing that variable I was refering to, I simply initialized it as variable:= Pointer(1); And it works with the function. The function is faulty that it returns nil if the variable is nil. Yet if I initialize it, it returns a pointer to the object in the host application like it supposed to. I am hoping that the Pointer(1) will not cause any leaks, at least I dont think it should. Since it's just type casting a value of 1 to a pointer, which to me is just garbage data that doesnt need to be freed.
----- Original Message ----- From: "Rob Kennedy" <[EMAIL PROTECTED]> To: "Delphi-Talk Discussion List" <[email protected]> Sent: Tuesday, October 25, 2005 12:32 AM Subject: Re: where does the memory go > 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 > __________________________________________________ Delphi-Talk mailing list -> [email protected] http://www.elists.org/mailman/listinfo/delphi-talk
