<p dir="ltr">Hi Sven,</p>
<p dir="ltr">This design means that old classes need to be subclassed in order to enable this feature, correct? <br>
Would it be possible instead of declaring this on the class definition, to declare it on the actual variable? Something like</p>
<p dir="ltr">var aArcObj: TFooObj; refcounted;</p>
<p dir="ltr">Or similar.</p>
<p dir="ltr">This would allow the same classes to be used both ways.</p>
<p dir="ltr">Also how about a smart pointer record implementation with generics? Is it possible ?</p>
<p dir="ltr">Thanks,<br>
Chriss</p>
<div class="gmail_quote">Στις 21 Σεπ 2014 2:12 μ.μ., ο χρήστης "Sven Barth" <<a href="mailto:pascaldragon@googlemail.com">pascaldragon@googlemail.com</a>> έγραψε:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 20.09.2014 13:42, Sven Barth wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 20.09.2014 13:11, Peter Popov wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Please do not reference count TObject. This is a uniquely bad and<br>
unnecessary idea. I will switch to ANSI C if you guys do it<br>
</blockquote>
<br>
Please enlighten me why you think it is bad. Give reasons and don't be<br>
like "a farmer doesn't eat what he doesn't know" (rough translation of a<br>
German proverb).<br>
</blockquote>
<br>
One just needs to provoke the others correctly and then the discussion starts. Great! :)<br>
<br>
I've thought about the topic a bit more (I've already thought about it quite much in the past months) and came up with the following "RFC":<br>
<br>
- No reference counting for classes by default<br>
- Reference counting can be introduced to a part of the class hierarchy by declaring it as "refcounted" like this (Note: syntax is not final, but is based on other syntaxes we have (namely "sealed" and "abstract")):<br>
<br>
=== code begin ===<br>
<br>
type<br>
TARCObject = class refcounted(TObject)<br>
<br>
end;<br>
<br>
=== code end ===<br>
<br>
- a reference counted class (and its child classes) would include a reference count field that the compiler knows how to access (for automatic reference counting) and which can be accessed through RTTI (for manual reference counting); it is *not* exposed as a regular field as this might lead to identifier conflicts<br>
- only *variables* (or parameters) that have a reference counted class as its type will be subject to ARC; in extension this means that assignments from/to a variable of a not reference counted base class (e.g. TObject) will *not* change the reference count. Take this code for example:<br>
<br>
=== code begin ===<br>
<br>
function CreateObject: TObject;<br>
begin<br>
Result := TARCObject.Create;<br>
end;<br>
<br>
=== code end ===<br>
<br>
This will most likely result in "CreateObject" returning an instance to class that is already freed (because the only reference inside "CreateObject" is a (hypothetical) temporary of type "TARCObject" which goes out of scope once "CreateObject" returns)<br>
- to remedy this TObject is extended with non-virtual methods that allow manual reference counting and would rely on the RTTI data I mentioned (let's call the methods AddRef, Release, IsReferenceCounted and RefCount for now, which can also be used to hook up the reference counting of IUnknown interfaces); the code from above would then look like this to make it safe:<br>
<br>
=== code begin ===<br>
<br>
function CreateObject: TObject;<br>
begin<br>
Result := TARCObject.Create;<br>
Result.AddRef;<br>
end;<br>
<br>
=== code end ===<br>
<br>
- TObject.Free would be extended to take reference counting into account as well. If the object is reference counted (IsReferenceCounted returns true) it will call Release and otherwise it will continue to Destroy.<br>
- there would be a TARCObject declared in System which is a direct descendant of TObject, but with reference counting enabled; same maybe also for TInterfacedObject<br>
- all classes can now have operator overloads as well though it should be warned in the documentation that non-reference counted objects might result in memory leaks there<br>
- this now only leaves the problems of cycles; take this code:<br>
<br>
=== code begin ===<br>
<br>
type<br>
TSomeClass = class(TARCObject)<br>
Children: specialize TList<TSomeClass>;<br>
Owner: TSomeClass;<br>
constructor Create(aOwner: TSomeClass);<br>
end;<br>
<br>
constructor TSomeClass.Create(aOwner: TSomeClass);<br>
begin<br>
Children := specialize TList<TSomeClass>.Create;<br>
Owner := aOwner;<br>
if Assigned(Owner) then<br>
Owner.Children.Add(Self);<br>
end;<br>
<br>
procedure Test;<br>
var<br>
t1, t2: TSomeClass;<br>
begin<br>
t1 := TSomeClass.Create(Nil);<br>
t2 := TSomeClass.Create(t1);<br>
// do something<br>
end;<br>
<br>
=== code end ===<br>
<br>
Now once Test is left it would leave the instances which were assigned to t1 and t2 hanging, because they have references to each other.<br>
There are (as far as I see) three ways to solve this:<br>
* provide a way to break the circle (in this example e.g. setting Owner to Nil before leaving Test; this is what Delphi provides with the DisposeOf virtual method)<br>
* introduce weak references which would disable reference counting, e.g.:<br>
<br>
=== code begin ===<br>
<br>
type<br>
TSomeClass = class(TARCObject)<br>
// ...<br>
Owner: TSomeClass weak;<br>
// ...<br>
end;<br>
<br>
=== code end ===<br>
<br>
Now the "TSomeClass.Create(t1)" line in "Test" wouldn't increase the reference count of "t1" further and thus both class instances would be destroyed after "Test" is left.<br>
* provide a possibilty to execute a cycle detection algorithm during the Release part of the reference counting; this has the benefit of avoiding the need for "weak", but there would be the problem that the algorithm can be potentially expensive especially with large object instance hierarchies (think LCL here) and this would also need to be executed for *each* decrement of the reference count, thus for both the automated one (which till now could have been rather efficient) and the manual one.<br>
<br>
I think this approach would allow the best of both worlds and if someone really wants to have TObject reference counted (and face the potential consequences), then he/she should simply adjust his/her RTL. ;)<br>
<br>
Regards,<br>
Sven<br>
______________________________<u></u>_________________<br>
fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org" target="_blank">fpc-devel@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel" target="_blank">http://lists.freepascal.org/<u></u>cgi-bin/mailman/listinfo/fpc-<u></u>devel</a><br>
</blockquote></div>