0

Given the following answer to a related question (https://stackoverflow.com/a/22086392/1420752) are objects reference counted in Windows-targeted Delphi applications?

I.e.:

Q1A does the following object have a reference count of 2 after the second statement?

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

Q1B Following on from the above, will assigning o1 to nil drop the reference count to 1?

o1 := nil;

Q1C Again following on, will assigning o2 to nil drop the reference count to 0?

o2 := nil;

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

Q1E If reference counting does not automatically free the memory associated with an object, then what precisely is the point of reference counting in Delphi (e.g., is it to help trace memory leaks)?

Community
  • 1
  • 1
magnus
  • 4,031
  • 7
  • 26
  • 48
  • 4
    Unfortunately you have been misled by that other answer. – David Heffernan Feb 28 '14 at 07:08
  • 3
    There's no reference counting on object instances in the Desktop targets (Win32/MacOS) of Delphi - only in the mobile targets (Android/iOS). – HeartWare Feb 28 '14 at 07:10
  • 1
    It might be vital to mention that while **classes** are not reference counted on _Windows desktop compiler_ s, **strings** and **dynamic arrays are**. I know that the original question _explicitly mentioned class instances_, but when I first met Delphi/Object Pascal with a C/C++ background (as the OP seems to be), that was a source of a series of misunderstandings for me. One might also mention **interfaces** which **can be** (mis?) **used as reference counted objects** . – mg30rg Feb 28 '14 at 08:37
  • @DavidHeffernan: I do not see anything in the answers or comments of the question that would have been misleading. Every time ARC was mentioned in relation to objects, all of the answers/comments made sure to say that object ARC only applied in mobile platforms, not desktop platforms. So if the OP of this question is confused by object ARC, he/she did not read the other answers/comments very well. – Remy Lebeau Mar 01 '14 at 01:22
  • @Remy Read CantChooseUsername's answer. It asserts that objects are ref counted. It doesn't mention desktop compilers or mobile. – David Heffernan Mar 01 '14 at 08:52
  • @DavidHeffernan: OK, I did not see that earlier. HeartWare's comment (and now my own as well) to that same answer clarifies the difference between desktop and mobile. – Remy Lebeau Mar 01 '14 at 09:56

2 Answers2

11

Object instances are not reference counted under any desktop platform, including Windows. Objects are reference counted only under mobile platforms (iOS, Android). So questions Q1A-E are moot.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
3

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

  • Q1A: yes, it would be 2
  • Q1B: yes, it would be down to 1
  • Q1C: yes, it would be 0 and the object would be freed automatically
  • Q1D: see Q1C
  • Q1E: See Q1C

Note that interfaces (and thus the objects implementing them) are refcounted, even in the desktop compilers. The above answers also apply to such interfaces.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • The question specifically asks about desktop compilers. You've therefore answered a question that was not asked. Feel free to ask it, and answer it. – David Heffernan Feb 28 '14 at 08:06
  • I know I did not just answer the question. I just provided some extra info. – Rudy Velthuis Feb 28 '14 at 08:48
  • @RudyVelthuis I think that is the main purpose of comments. (But **you have provided useful information** .) – mg30rg Feb 28 '14 at 08:52
  • @Rudy You should delete this answer since it is unrelated to the question. Ask another question about mobile compilers, and add your answer there. SO is about focused answers to specific questions. – David Heffernan Feb 28 '14 at 09:00