Edwin Yip
Edwin Yip

Reputation: 4220

Do I have to worry about integer values assigned to TList become invalid?

Lets say I have the following procedure, I have the following questions marked as Q1, Q2, and so on in the comments of the code:

var
  winHandle: THandle;
...
// At this point, assume aList is empty.
winHandle := FindWindow(...);
aList.Add(Pointer(winHandle));
// Now THandle(aList[0]) = winHandle.

// After this point, variable `winHandle` will go out of scope thus become invalid, so:
// Q1: Do I have to worry about that the value of THandle(aList[0])
//     will be erased in the memory thus become invalid?
// Q2: If yes, what can I do to keep the validity of the value of THandle(aList[0])?
// Q3: If not, do I have to free the memory of THandle(aList[0]) when freeing aList?

I think I lack some of the knowledge about Delphi's memory management. Thanks.

Upvotes: 1

Views: 173

Answers (1)

David Heffernan
David Heffernan

Reputation: 613412

Q1 No. An integer is a value type. When you assign an integer, you take a copy of its value. As it happens, in this case, TList accepts Pointer, but Pointer is simply an address, a value type, and conceptually treated identically as an integer value type for the purpose of assignment.

Q3 You do not have to free any memory because the integers are values rather than references.

Upvotes: 2

Related Questions