[Code1:]
Code: Select all
class CCC{
__Init() {
this.Ptr:=ObjPtr(this)
}
self=>ObjFromPtr(this.Ptr)
}
c:=CCC()
c2:=c.self
OutputDebug(ObjPtr(c2))
OutputDebug(ObjPtr(c))
Code: Select all
class CCC{
__Init() {
this.Ptr:=ObjPtr(this)
}
self=>ObjFromPtr(this.Ptr)
}
c:=CCC()
c2:=c.self
OutputDebug(ObjPtr(c2))
OutputDebug(ObjPtr(c))
Yes, I suddenly find this works well when I test for other methods which may work.
Shouldn't the reference of c2's object increase immediately the first time any object is assigned to c2 from whichever function? Writing codes as usual, seems more intuitive. So now I doubt that maybe there really is something wrong with ObjFromPtr still, otherwise it should have worked at least in normal case. Currentlly, c2 surprisingly called __delete() twice in Code1, which seems unintentional.Because you're making a reference using ObjFromPtr without increasing the reference count,
No, if you are using ObjPtr and ObjFromPtr then it is assumed you know what you are doing and are accounting for the reference count. There are situations where you might want to use the object without increasing the reference count, in which case ObjFromPtr is necessary (creating the object without increasing ref count). For example, if a DllCall returns an object from an external library, usually the library has already increased the reference count for the object and then gives ownership to you, which means you will have to decrease the ref count when you are done but not increase the count when you are starting to use it.Shouldn't the reference of c2's object increase immediately the first time any object is assigned to c2 from whichever function? Writing codes as usual, seems more intuitive. Maybe there really is something wrong with ObjFromPtr still, otherwise it should have worked at least in normal case. Currentlly, c2 is surprisingly deleted twice in Code1, which seems unintentional.
Code: Select all
__Init() {
this.Ptr:=ObjPtr(this), ObjAddRef(this.Ptr)
}
Thank you for your answer. Perhaps how the reference counter works underneath, is really hard to exactly know.Descolada wrote: ↑20 Aug 2023, 01:09No, if you are using ObjPtr and ObjFromPtr then it is assumed you know what you are doing and are accounting for the reference count. There are situations where you might want to use the object without increasing the reference count, in which case ObjFromPtr is necessary (creating the object without increasing ref count). For example, if a DllCall returns an object from an external library, usually the library has already increased the reference count for the object and then gives ownership to you, which means you will have to decrease the ref count when you are done but not increase the count when you are starting to use it.
Code: Select all
class AAA{
__New() {
this.Ptr:=ObjPtr(this)
OutputDebug(this.Ptr "`n")
}
__Delete() {
OutputDebug(this.Ptr " is being deleted`n")
ObjAddRef(this.Ptr)
}
}
a:=AAA()
b:=a.Ptr
a:=""
b:=ObjFromPtr(b)
OutputDebug(b.Ptr " is alive again!`n")
So, is that to say, c2:=c.self will increase the reference counter from 0 to 1, rather than from 1 to 2? Is this by design?
Code: Select all
class CCC{
__Init() {
this.Ptr:=ObjPtr(this)
}
self=>ObjFromPtr(this.Ptr)
__Delete() {
OutputDebug('deleting')
}
}
c:=CCC()
c2:=c.self
Persistent()
Interesting. I wonder whether a filesystem analogy would be appropriate here. From the point of view of the filesystem, as i understand it, *what it is* for a file to "exist" is nothing more than for there to exist at least one reference to a particular inode (at least one "hardlink"). So, for files, to exist is to be referred to, to go out of existence is to cease being referred to (compare: "The Egyptians believed that you die twice. Once when you take your final breath, and then again the last time someone says your name"). So the idea of a file with zero reference count is just a contradiction in terms. If all references (hardlinks) to a file are removed, there may still exist at the inode the same sequence of 1s and 0s that once constituted an actual file, but that file itself no longer exists, all the 1s and 0s left behind at that inode is just the "remnants" of a file.lexikos wrote: ↑20 Aug 2023, 20:02An object's reference count can't ever be zero, because that should mean the object no longer exists, in which case you can't read its reference count.
AutoHotkey doesn't decrement the reference count below 1 when you Release, because it has to call __delete. __delete needs a reference to the object, which means it must have a non-zero count. If it were to increment from 0 to 1 and then back to 0 when __delete finishes, it would be "deleted" again.
If the object has already been deleted and you still have a pointer, that pointer is invalid. It points to indeterminate data. If you try to read a "reference count" from that, you could get anything.
On objects with a reference count of zero - Raymond Chen
No.V2User wrote:So, is that to say, c2:=c.self will increase the reference counter from 0 to 1, rather than from 1 to 2?
Users browsing this forum: diegg1739, Frogrammer, wilkster and 73 guests