Page 1 of 1

[Objects] - Clone() bug?

Posted: 14 Sep 2018, 18:20
by User
Hi,

from the code example below, a cloned object keeps referencing the same object:

Code: Select all

x := []
y := []

y[1, "type"]   := "y1 type"
y[1, "option"] := "y1 option"
y[1, "header"] := "y1 header"

x["YObject"] := y	;stores object "y" reference in x["YObject"]

YReference := x["YObject"]

z := YReference.Clone()

z[1, "header"] := "z1 header"
;the line above modifies y[1, "header"] to "z1 header" (but it shouldn't since "z" contains reference of a "y" clone!)

msgbox, % ""
. y[1, "type"] " - " y[1, "option"] " - " y[1, "header"]  "`n"
. z[1, "type"] " - " z[1, "option"] " - " z[1, "header"]  "`n"

Re: [Objects] - Clone() bug?  Topic is solved

Posted: 14 Sep 2018, 18:25
by Flipeador

Re: [Objects] - Clone() bug?

Posted: 19 Sep 2018, 00:00
by User
Really? Who marked this thread as solved? Anyway, it wasn't me!

(Obs): I didn't test the function and I really don't know if it is reliable or even safe to be used! Thanks!

Re: [Objects] - Clone() bug?

Posted: 19 Sep 2018, 05:11
by Flipeador
If you mean ObjFullyClone, I do not see why it would not be reliable or safe to be used. It seems ok.
:wave:

Re: [Objects] - Clone() bug?

Posted: 19 Sep 2018, 05:15
by nnnik
well .() seems to be deprecated and %%() prefered but with the exception of that it seems solid.

Re: [Objects] - Clone() bug?

Posted: 19 Sep 2018, 05:25
by Helgef
It doesn't handle circular references. ( meaning it is neither safe nor reliable for general use)

Cheers.

Edit:
Not tested,

Code: Select all

ObjFullyClone(obj, z := "")
{
   	if !z
		z := []
	nobj := obj.Clone()
    for k,v in nobj
        if IsObject(v) && !z.haskey(v)
            z[v]:="", nobj[k] := A_ThisFunc.(v, z)
    return nobj
} ; https://autohotkey.com/boards/viewtopic.php?f=74&t=28542&p=157603#p157603

Re: [Objects] - Clone() bug?

Posted: 19 Sep 2018, 09:52
by nnnik
Well if you want to be really precise then the function doesn't handle objects beyond a specific depth since it uses a recursion.

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 03:01
by Helgef
That is more precise, yes :thumbup: . The problem with circular references is that it causes infinite recursion.

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 03:16
by nnnik
No if I have an object thats deeper than 600 levels AHK v1.1 is going to exit.

Code: Select all

root := a := []
Loop 700
	a := a.1 := []

b := ObjFullyClone(root)

ObjFullyClone(obj, z := "")
{
   	if !z
		z := []
	nobj := obj.Clone()
	for k,v in nobj
		if IsObject(v) && !z.haskey(v)
			z[v]:="", nobj[k] := A_ThisFunc.(v, z)
	return nobj
} ; https://autohotkey.com/boards/viewtopic.php?f=74&t=28542&p=157603#p157603

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 03:32
by Helgef
No
What do you mean No? That is what you said, and what I agreed with, i.e., the excessive recursion is the problem. I clearified that circular references causes this.

Cheers.

Edit: I suppose you mean that my version of the function doesn't fix the function, it was only mean to handle circular reference, so ofc it is not going to handle excessive recursion due to very deep objects. You can handle the deep object by making a non recursive function, but you can still run out of memory if the object being cloned is sufficiently deep.

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 07:55
by nnnik
Yeah but thats something thats not inherently the problem of this function but rather a problem in the way how our computers are limited.
Where as the stackoverflow from using recursion stems from bad practice.

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 07:59
by Helgef
The original function would never return even if this limitation didn't exist, so it is still a problem of the original function, which my version fixes. Your point about the depth issue is the most important point though, but I guess circular references is the most common case which will cause you to observe the issue, I could be wrong on that ofc.

Cheers.

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 08:19
by nnnik
No you're right about that - I just think it would be better if we avoided recursion in general.
Circular references are a seperate problem which has been adressed adequately by your code.

After that it's only handling .base objects, and using ObjRawSet/ObjRawGet and ObjNewEnum instead of the standard syntax.

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 09:18
by nnnik
Another thing I noticed is binary data.
Would that clone binary data correctly?

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 09:27
by Helgef
I do not think it does, clone that is, it will not be a problem in v2 if (when?) binary objects are added. Edit, perhaps not as trivially as I suggested :roll: .

Cheers.
nnnik wrote:Now I know why my FastMatrix library started throwing Access Violations left and right... I'm an idiot
:( . Glad you found the problem though :thumbup:

Re: [Objects] - Clone() bug?

Posted: 21 Sep 2018, 09:34
by nnnik
Now I know why my FastMatrix library started throwing Access Violations left and right... I'm an idiot