Is object reference counted in Windows-based Delphi applications, and if so, what is its purpose?

Given the following answer to a question related to it ( https://stackoverflow.com/a/166269/ ), are object references counted in Windows-based Delphi applications?

i.e:.

Q1A Does the next object have a reference count of 2 after the second statement?

o1 := TMyObject.Create; o2 := o1; 

Q1B . Following the above, setting o1 to nil , reset the reference count to 1?

 o1 := nil; 

Q1C Following on again, setting o2 to nil , reset the reference count to 0?

 o2 := nil; 

Q1D Moving forward, if the above is correct, and now the object has a reference count of 0, I understand that the compiler will NOT automatically free the object ( o2.Free should have been called earlier on o2 := nil above to prevent memory leak). Remember that I'm talking about a Windows target, not a mobile target with automatic link counting (ARC).

Q1E If link counting does not automatically free up memory associated with an object, then what exactly is the reference counting point in Delphi (for example, does it help track memory leaks)?

0
source share
2 answers

Object instances do not apply to calculations under any desktop platform, including Windows. Objects refer only to mobile platforms (iOS, Android). Therefore, questions Q1A-E are controversial.

+11
source

As Uli already said, objects are not counted in desktop compilers . But they exist in mobile compilers , and the answers to your questions for these compilers are:

  • Q1A: yes it will be 2
  • Q1B: Yes, it will be up to 1
  • Q1C: yes, it will be 0, and the object will be automatically freed.
  • Q1D: see Q1C
  • Q1E: see Q1C

Note that interfaces (and, therefore, objects that implement them) are even converted to desktop compilers. The above answers also apply to such interfaces.

+4
source

Source: https://habr.com/ru/post/972440/


All Articles