[Close]AddRef on released object - Manage Refference Count After __Delete

Get help with using AutoHotkey and its commands and hotkeys
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

[Close]AddRef on released object - Manage Refference Count After __Delete

22 Sep 2015, 06:37

Almost clear, this found could be used well I think, see posts below.

Code: Select all

class a{
	b:=new b(this)
}
class b{
	__New(instance){
		this[instance]:=true
		ObjRelease(&instance)
	}
	__Delete(){
		for instance in this
			ObjAddRef(&instance)
	}
}
c:=new a
return
Although b in a is released after a in the same seriou process which a is being / just freed, but using ObjAddRef(&a) in b, scprit can exit fine.
The released object is an empty object, remaining its base, and could be used as normal. Will it be reclaimed later when GC?
Momentary, then freed totally

Can I use this feature?
I think it could be used to create weak ref, even in ring-ref / loop-ref, and slave-reversal ref to master object. Without a complex handling process.
@lexikos !!
Last edited by RobertL on 24 Sep 2015, 02:14, edited 7 times in total.
我为人人,人人为己?
lexikos
Posts: 6214
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AddRef on released object

22 Sep 2015, 18:53

No. It's not a "feature". You're invoking undefined behaviour, like picking some random address and reinterpreting it as an object. The object has been freed and no longer exists; now there's just some 1s and 0s where it used to be.
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: AddRef on released object

22 Sep 2015, 19:07

@lexikos
I won't use a's content in b since it may be freed, I just want to free b after a which b is a member of a, and free what b contains - the a.
Means, ObjAddRef(&instance) won't cause error will be satisficed, especially when ObjAddRef(&instance) returns 1.

BTW, the freed a still has it base relationship, is it not freed totally or there is some leak?
lexikos wrote: there's just some 1s and 0s where it used to be
What does it mean? What does "s" represent? Ref count?
我为人人,人人为己?
lexikos
Posts: 6214
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AddRef on released object

22 Sep 2015, 22:43

I'm having difficulty understanding your first paragraph.
the freed a still has it base relationship
The freed a doesn't have anything, because it doesn't exist. In its place, there's just garbage data. Or maybe there's something else entirely, like a string or another object allocated at the same (or overlapping) address.

"s" represents plural (more than one)... :roll:
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: AddRef on released object

22 Sep 2015, 22:54

@lexikos
I wrote:the freed a still has it base relationship.
Means, inspect variable a could see its member - base by SciTE4AHK. I think a could work after AddRef - Although I won't do this
All other members was deleted.
Momentary, in the same / series process as a being freed in. See below.

lexikos wrote:there's just some 1s and 0s where it used to be
I know now :lolno: , there's just some binary data 010100.. remained.

The first paragraph I wrote: I just want to free b after a which b is a member of a, and free what b contains - the a.
Which should be handled on a's responsibility, before this found feature :lol: . Now could be handled by b itself without modify anything of a like a's __Delete


limit:the followed process of b to AddRef and free a should be the same / series process as a being freed in.
Otherwise this will fail:

Code: Select all

_a:=new a
d:=_a.b ;new added to keep 'a' after 'a' freed
_a:=""
d:=""  ;failed here. this will re-free 'a'.
And a will only been freed once. Could see this by its __Delete, been called once.

Use. Could use a's member to invoke a free chain. Not use __Delete because it's diffcult to manage, and could be overwrite by derive class. etc, etc...
Last edited by RobertL on 23 Sep 2015, 04:14, edited 1 time in total.
我为人人,人人为己?
lexikos
Posts: 6214
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AddRef on released object - nice!

23 Sep 2015, 03:42

I still don't understand you.

If you want to use ObjRelease to make a "weak" reference, you MUST call ObjAddRef an equal number of times before the object is fully deleted. You MUST NOT attempt to access memory at the object's address after the object is deleted.

The object's __Delete method is the last chance to do anything with that object. If you have previously decremented the reference count, you can correct it in __Delete. When the reference count reaches zero again, __Delete will be called again. If you don't implement __Delete (as you haven't in class a), you will have no way to know when your "uncounted" references to the object become invalid.

Is it a yearly tradition for you to mess with reference counting? ;)
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: AddRef on released object - nice!

23 Sep 2015, 03:58

lexikos wrote:you MUST call ObjAddRef an equal number of times
Yes, I understand. I think I did it carefully.
lexikos wrote:The object's __Delete method is the last chance to do anything with that object.
No, in this case above, __Delete method is NOT the last chance.
What I found the free process of a is, __Delete, delete every member of a, and, I use the __Delete of one member (which is b above) to correct refference count of a.
In __Delete of b, a isn't totally freed, depend on the order of b in a, so I said "the freed freeing a still has its base relationship" or its other member which didn't freed yet.
And b won't access / need a's member, the responsibility of b is to free other weak refference.

See Momentary, and limit in the previous post before yours.
Just now, I begin to understand the key.


I almost forget that topic, thank you. And this found is the new world for sure. @lexikos :dance:
我为人人,人人为己?
lexikos
Posts: 6214
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

23 Sep 2015, 16:54

No, in this case above, __Delete method is NOT the last chance.
Okay, if you want broken, unreliable scripts, by all means, ignore my advice. But don't post such scripts here.

If the object HAS BEEN DELETED, you can't access the object. What you're doing is accessing the GARBAGE left over after the object was destroyed. Like a corpse that you've dug up out of its grave.
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

23 Sep 2015, 19:19

@lexikos Sorry, I think you didnt' catch my point. I understand yours. I means I get the boundary, did some test, and can explain it.
Repeat, when __Delete is calling, the object is not freed yet, has not been deleted yet, even when returns from it, UNTIL all its members were deleted, Then destroy the object, the address is invalid.
Is it correct, and could be used for the purpose as below?
I add work in its member which be refferenced only by the object, this member is released before / in the same time as the object is released, so...

Thank you, whether you still misunderstand nor didn't point out which part I'm wrong... :cry:

If @trismarck is present, situation will be nice to me. But maybe you are too busy... :eh:
我为人人,人人为己?
lexikos
Posts: 6214
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

23 Sep 2015, 20:57

RobertL wrote:I understand [your point].
No, you clearly don't.
↑Is it correct, and could be used for the purpose as below?
No and no!

Specifically "[the object] has not been deleted yet" is incorrect.

What you are doing is like this:

Code: Select all

; Allocate a memory block.
pdata := DllCall("msvcrt\malloc", "ptr", 40, "ptr")

; Put a number into it.
NumPut(42, pdata+8, "int")

; Get the same number back.
MsgBox % NumGet(pdata+8, "int")  ; 42

; DELETE the memory block:
DllCall("msvcrt\free", "ptr", pdata)

; Get the same number back...?
MsgBox % NumGet(pdata+8, "int")
On my system, both boxes show "42". Maybe it will do the same on yours, maybe not. It is unreliable.

If I use pdata+0, I find that it is overwritten immediately, and I don't get "42" in the second box. But maybe you'll get different results.
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

23 Sep 2015, 21:23

@lexikos Yes, yes, I know conception of wild pointer. I won't access it after it is released, but can do this before - when releasing its member, the object is still exsit, un-freed.

Code: Select all

;When refference count is zero
a.__delete()	;'Last' chance to access 'a'
for k in a
	objDetele(a,k)	;Everything I talked about occurs here
free(a)	; DELETE the memory block:
a:=""		;Could add this
lexikos wrote:Specifically "[the object] has not been deleted yet" is incorrect.
Maybe I’m wrong, I can't guarantee the background process. I'll currently (already) use it like the DEMO shows, until it breaks.
DEMO:

Code: Select all

class A{
	__Delete(){
		MsgBox __Delete of _a.
	}
	_1:=1
	_2:=new b(this)
	_3:=3
}
class b{
	__New(instance){
		this.Instance:=instance
		ObjRelease(&instance)
	}
	__Delete(){
		MsgBox % "'_a' is deleting, but not totally at this period - release its member, could see its unreleased member and base - '" this.Instance.base.__Class "'"
		instance:=this.Instance
		ObjAddRef(&instance)

		;Other work which don't need 'a'.
		;...
	}
}
_a:=new A
MsgBox Begin to delete _a.
_a:=""
MsgBox Now can't access '_a' any more, because it becomes wild pointer.
ExitApp
Thanks thanks :salute:
我为人人,人人为己?
lexikos
Posts: 6214
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

23 Sep 2015, 21:48

I won't access it after it is released
You are accessing it after it is released.
when releasing its member, the object is still exsit, un-freed
I see now; the object has been partially destroyed but its main memory block has not yet been freed. It is not safe to access the "object" at this stage (it is no longer an object, just a memory block). Don't do it.
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

23 Sep 2015, 22:16

"when releasing its member", I won't access its member neither.
The only thing need to do with object, is to set refference count (AddRef) to keep its balance when release (refCount-1) the object 'again' from b by clear b's member, like a normal Add then Release operate, nothing harm will happen.
Then b can do many meaningful work alone.

@lexikos Finally, you get it. :crazy:
我为人人,人人为己?
lexikos
Posts: 6214
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

24 Sep 2015, 00:37

Don't you ever listen? It's not safe. Don't do it.

If you increment the reference counter on the "object" (which is not an object anymore, but some junk in memory), either it will have no effect at all, or AutoHotkey will at some point try to free it, and crash or cause other wrong behaviour. There is absolutely no reason to mess with the "reference count" of an "object" which has already been destroyed.
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: AddRef on released object - Manage Refference Count After __Delete -nice!

24 Sep 2015, 02:13

Eh... I calm down now... Yes, understand, it's truely unknown... :sick: Thank you. :oops: @lexikos

I found new way to original method replace this one. Store the address of a, but not the reference in b, avoid reverse referrence, when a freed disable and clear other entrances in address to a.
我为人人,人人为己?

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot] and 176 guests