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

Get help with using AutoHotkey and its commands and hotkeys
User
Posts: 219
Joined: 26 Jun 2017, 08:12

[Objects] - Clone() bug?

Post by User » 14 Sep 2018, 18:20

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"
User avatar
Flipeador
Posts: 1018
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

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

Post by Flipeador » 14 Sep 2018, 18:25

Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User
Posts: 219
Joined: 26 Jun 2017, 08:12

Re: [Objects] - Clone() bug?

Post by User » 19 Sep 2018, 00:00

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!
User avatar
Flipeador
Posts: 1018
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: [Objects] - Clone() bug?

Post by Flipeador » 19 Sep 2018, 05:11

If you mean ObjFullyClone, I do not see why it would not be reliable or safe to be used. It seems ok.
:wave:
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
nnnik
Posts: 3326
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Objects] - Clone() bug?

Post by nnnik » 19 Sep 2018, 05:15

well .() seems to be deprecated and %%() prefered but with the exception of that it seems solid.
Recommends AHK Studio
Helgef
Posts: 3186
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Objects] - Clone() bug?

Post by Helgef » 19 Sep 2018, 05:25

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
User avatar
nnnik
Posts: 3326
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Objects] - Clone() bug?

Post by nnnik » 19 Sep 2018, 09:52

Well if you want to be really precise then the function doesn't handle objects beyond a specific depth since it uses a recursion.
Recommends AHK Studio
Helgef
Posts: 3186
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Objects] - Clone() bug?

Post by Helgef » 21 Sep 2018, 03:01

That is more precise, yes :thumbup: . The problem with circular references is that it causes infinite recursion.
User avatar
nnnik
Posts: 3326
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Objects] - Clone() bug?

Post by nnnik » 21 Sep 2018, 03:16

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
Recommends AHK Studio
Helgef
Posts: 3186
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Objects] - Clone() bug?

Post by Helgef » 21 Sep 2018, 03:32

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.
User avatar
nnnik
Posts: 3326
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Objects] - Clone() bug?

Post by nnnik » 21 Sep 2018, 07:55

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.
Recommends AHK Studio
Helgef
Posts: 3186
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Objects] - Clone() bug?

Post by Helgef » 21 Sep 2018, 07:59

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.
User avatar
nnnik
Posts: 3326
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Objects] - Clone() bug?

Post by nnnik » 21 Sep 2018, 08:19

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.
Recommends AHK Studio
User avatar
nnnik
Posts: 3326
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Objects] - Clone() bug?

Post by nnnik » 21 Sep 2018, 09:18

Another thing I noticed is binary data.
Would that clone binary data correctly?
Recommends AHK Studio
Helgef
Posts: 3186
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Objects] - Clone() bug?

Post by Helgef » 21 Sep 2018, 09:27

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:
Last edited by Helgef on 21 Sep 2018, 14:46, edited 2 times in total.
User avatar
nnnik
Posts: 3326
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Objects] - Clone() bug?

Post by nnnik » 21 Sep 2018, 09:34

Now I know why my FastMatrix library started throwing Access Violations left and right... I'm an idiot
Recommends AHK Studio
Post Reply

Return to “Ask For Help”