Advertisement

Objects with handle member can get garbage collected after array sort

Started by May 15, 2021 08:17 PM
4 comments, last by Miss 3 years, 6 months ago

The following script can cause some of the objects of “Foo” to be destructed during sorting.

class Bar {}
class Foo
{
	int m_i;
	Bar@ m_bar; // comment this out to make the problem disappear

	Foo(int i) { m_i = i; }
	~Foo() { print("dtor " + m_i); }
}

bool SortFunc(const Foo@ &in a, const Foo@ &in b)
{
	return b.m_i % 7 == 0;
}

array<Foo@> g_arr;

void Main()
{
	print("start of main");

	for (int i = 0; i < 500; i++) {
		g_arr.InsertLast(Foo(i));
	}

	g_arr.Sort(SortFunc);

	print("end of main");
}

It seems to be due to the existance of m_bar. Changing that to not be a handle (eg.Bar m_bar”) will make the problem disappear, as well as not calling Sort.

Example output:

[    ScriptEngine] [22:10:20]  Loaded 'Plugin_Repro'
[   ScriptRuntime] [22:10:20]  start of main
[   ScriptRuntime] [22:10:20]  dtor 124
[   ScriptRuntime] [22:10:20]  dtor 217
[   ScriptRuntime] [22:10:20]  dtor 282
[   ScriptRuntime] [22:10:20]  dtor 335
[   ScriptRuntime] [22:10:20]  dtor 381
[   ScriptRuntime] [22:10:20]  dtor 422
[   ScriptRuntime] [22:10:20]  dtor 459
[   ScriptRuntime] [22:10:20]  end of main
[   ScriptRuntime] [22:10:20]  dtor 494

The indices that it destructs can be different every time. I also find the destructor after “end of main” to be quite odd.

This is using the latest SVN version of Angelscript as well as the latest SVN version of scriptarray (although the function names modified to have capital letters)

Thanks for the report.

I'll need to investigate this.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

I've identified the cause.

While sorting the entries in the array, an entry is temporarily referred to from two indices at the same time, and when the garbage collector invokes the enum references callback the array is reporting the wrong number of references to the object leading the gc to think the object are locked in a circular reference that needs to be broken in order to destroy the garbage.

I'm still thinking about the best solution for this problem. But for now a simple solution is to change the sort method to clear the entry it is currently moving right after copying the value to the local variable tmp. Another work around is to temporarily turn off the auto garbage collection while sorting the array.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've fixed this in revision 2731.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thank you so much!

This topic is closed to new replies.

Advertisement