new Class() fails silently after a reference is pushed in an array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

new Class() fails silently after a reference is pushed in an array

Post by kyuuuri » 26 Jan 2022, 18:50

Hello, I know this is a weird bug but I could only reproduce with this code.

Code: Select all

class Container {
	_bannedElements := []
	elementList := []
	
	__New() {
	}
	
	elements() {
		this.elementList := []
		elementList := []
		loop, 10
		{
			random, rand, 1, 10
			element := new Element(rand)
			if (!element)
				msgbox % "no element`naddress: " element.address "`nisObject: " isObject(element) "`ntest: " element.test
			
			if (!this.helloIsBanned(element))
				elementList.push(element)
		}
		this.elementList := elementList
		return this.elementList
	}
	
	helloIsBanned(elem) {
        for key, element in this._bannedElements
		{
            if (element.address = elem.address)
                return true
		}
        return false
    }

    banElement(element) {
        this._bannedElements.push(element.address)
    }

    removeBans() {
        this._bannedElements := []
    }
}

class Element {
	address := 0
	test := 123

	__New(address) {
        this.address := address
    }
}

container := new Container()

elements := container.elements()

msgbox % "First list of elements`n`n" obj2String(elements)
sleep, 100
bannedNumber := elements[2]
container.banElement(elements[2])
sleep, 100
elements := container.elements()
msgbox % "List of elements after ban of number: " bannedNumber "`n`n" obj2String(elements)
container.removeBans()
elements := container.elements()
msgbox % "List of elements after removing ban`n`n" obj2String(elements)



Obj2String(Obj,FullPath:=1,BottomBlank:=0, delimiter := "`n")
{
	static String,Blank
	if (FullPath=1)
		String:=FullPath:=Blank:=""
	if (IsObject(Obj)) {
		for key, value in Obj {
			if (IsObject(value))
				Obj2String(value, FullPath "." key, BottomBlank)
			else {
				if (BottomBlank=0)
					String .= FullPath "." key " = " value delimiter
				else if (value != "")
					String .= FullPath "." key " = " value delimiter
				else
					Blank .= FullPath "." key " =" delimiter
			}
		}
	}
	return String Blank
}
What it should do
It should these things in the following order:
1. Show a list of elements
2. Ban an element (this prevents it from being added to the list the next time we request a list of elements)
3. Show a list of elements again without any banned element
4. Remove the ban
5. Show a list of elements again

What it does
It does these things in the following order:
1. Shows a list of elements in the msgbox
2. Bans an element (this prevents it from being added to the list the next time we request a list of eleemnts)
3. 10 msgboxes are thrown showing that the the new Element() failed and the var is empty. The msgbox shows an empty list, sometimes this list has 1 element
4. Removes the ban (I think)
5. Shows an empty list, sometimes this list has 8 empty elements

What I tried
I think this is related to the references of the objects but I don't see how this can affect the script in a way that it is not able to create a new class anymore.


kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: new Class() fails silently after a reference is pushed in an array

Post by kyuuuri » 26 Jan 2022, 19:12

swagfag wrote:
26 Jan 2022, 19:00

Code: Select all

#Warn ClassOverwrite
Oh okay, never heard of #Warn. It's amazing how after so many years I keep learning more and more about AHk, thank you!

So this means that a variable box and a class box can overwrite each other. This is crazy, I never thought about it. Also there is non case-sensitive variables thing here that makes Box = box

Does this happens in languages like C++?

I know this doesn't happen in other script languages like JS.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: new Class() fails silently after a reference is pushed in an array

Post by amateur+ » 26 Jan 2022, 19:29

Also could you explain, someContainer._bannedElements is an array of what data? Elements or addresses of elements?
Spoiler
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: new Class() fails silently after a reference is pushed in an array

Post by kyuuuri » 26 Jan 2022, 19:37

amateur+ wrote:
26 Jan 2022, 19:29
Also could you explain, someContainer._bannedElements is an array of what data? Elements or addresses of elements?
Spoiler
And that is a big typo hahaha, my bad. I was reducing the size of the script to make the smallest code possible to reproduce the problem and I made that typo.

Code: Select all

elementIsBanned(element) {
        for key, address in this._bannedAddresses
		{
            if (address = element.address)
                return true
		}
        return false
    }

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: new Class() fails silently after a reference is pushed in an array

Post by swagfag » 29 Jan 2022, 16:04

in c++, classes are just types, not actual objects u could reassign
in javascript, no idea

Post Reply

Return to “Ask for Help (v1)”