Page MenuHomeFeedback Tracker

Cannot Remove() NULL key from map that uses non-primitive keys if two or more keys become NULL
Acknowledged, NormalPublic

Description

Like the title says. If two or more keys in a map that uses any non-primitive key become NULL for any reason, then it is not possible to Remove(NULL) these elements from the map. The workaround is to use RemoveElement(n) instead.

As to why this can be a problem with unexpected consequences that will even lead to a CTD, see https://feedback.bistudio.com/T163613

Details

Severity
None
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Category
Scripting
Steps To Reproduce

Minimal example:

class MyTestClass
{
}

map<MyTestClass, bool> test();

MyTestClass c1();
MyTestClass c2();

test.Insert(c1, true);
test.Insert(c2, false);

delete c1;
delete c2; //! Now we have two NULL keys in the map

MyTestClass k;
bool v;
int n;

Print(test.ToString() + " count " + test.Count());

for (n = 0; n < test.Count(); n++)
{
	v = test.GetElement(n);

	if (v)
	{
		k = test.GetKey(n);
		Print("Removing element with key " + k);
		test.Remove(k); //! This will fail because there are two NULL keys in the map
	}
}

Print(test.ToString() + " count " + test.Count());

for (n = 0; n < test.Count(); n++)
{
	k = test.GetKey(n);
	v = test.GetElement(n);
	Print("Element " + n + " key " + k + " value " + v);
}

for (n = 0; n < test.Count(); n++)
{
	v = test.GetElement(n);

	if (v)
	{
		k = test.GetKey(n);
		Print("Removing element " + n);
		test.RemoveElement(n);  //! This will work
	}
}

Print(test.ToString() + " count " + test.Count());

Will print the following:

SCRIPT       : map<MyTestClass,bool><acf3fc00> count 2
SCRIPT       : Removing element with key NULL
SCRIPT       : map<MyTestClass,bool><acf3fc00> count 2
SCRIPT       : Element 0 key NULL value true
SCRIPT       : Element 1 key NULL value false
SCRIPT       : Removing element 0
SCRIPT       : map<MyTestClass,bool><acf3fc00> count 1

Event Timeline

lava76 created this task.Jul 20 2022, 3:04 PM

Nice find, @lava76! Thanks for digging in and finding the root cause of T163613.

Geez changed the task status from New to Awaiting internal Testing.Jul 20 2022, 3:38 PM
Geez changed the task status from Awaiting internal Testing to Confirmed Internally.Jul 20 2022, 3:46 PM
Geez changed the task status from Confirmed Internally to Acknowledged.Jul 20 2022, 4:13 PM

I was half wrong actually, literally. There is only one NULL pointer key needed in the map for it to be not removable with map.Remove(NULL), as that would try to remove literal NULL from the map, not the key that originally pointed to an instance, but now points to NULL. Effect stays the same though.