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

别等时光非礼了梦想. 提交于 2020-01-05 07:17:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/59342365/do-i-have-to-worry-about-integer-values-assigned-to-tlist-become-invalid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!