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.
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.
If you were to
read the docs for ObjPtr then you'd understand what you did wrong: namely you created a pointer for an object without increasing the reference count for said object. Instead you should use either
Code: Select all
__Init() {
this.Ptr:=ObjPtr(this), ObjAddRef(this.Ptr)
}
or using ObjFromPtrAddRef instead of ObjFromPtr. I think ObjFromPtrAddRef would be the preferred way, because if you want to get multiple references to the object then only one ObjAddRef isn't enough.
AHK objects decrease the ref count automatically on destruction so in this case ObjRelease isn't necessary.
You should probably start posting your bug reports in the help section.