Page MenuHomeFeedback Tracker

Ref Counting when Method parameter is a Managed child
Closed, ResolvedPublic

Description

The search function didnt yield a result so i created a new ticket.

When using a thread (for GUI purpose for example) with a function which accepts a parameter that is inherited from Managed, the ref counter is not increased and cause a cleanup of the instance that was given via the parameter.

Details

Severity
None
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Category
General
Steps To Reproduce
[EntityEditorProps(category: "GameScripted/ScriptWizard", description: "ScriptWizard generated script file.")]
class SCR_MyClassClass : ScriptComponentClass
{
	// prefab properties here
}

class ClassA : Managed
{

	void OnSuccess()
	{
		Print("it ran");
	}

	//------------------------------------------------------------------------------------------------
	void ClassA()
	{
	}

}

void DoSomethingThread(notnull ClassA param)
{
        int i  = param.GetRefCount();
	Print(i.ToString());
	Sleep(5000);
        //here it can happen that param was cleaned up and is null
	i  = param.GetRefCount();
	param.OnSuccess();
}



	//------------------------------------------------------------------------------------------------
	

class SCR_MyClass : ScriptComponent
{

	override void EOnInit(IEntity owner)
	{
		ClassA test = new ClassA();
		
		thread DoSomethingThread(test);
	}
	
	override void OnPostInit(IEntity owner)
	{
		SetEventMask(owner, EntityEvent.INIT);
	}
	//------------------------------------------------------------------------------------------------
	void SCR_MyClass(IEntityComponentSource src, IEntity ent, IEntity parent)
	{
	}
	//------------------------------------------------------------------------------------------------
	void ~SCR_MyClass()
	{
	}

}

Add the component to an entity.

Event Timeline

TheMasterofBlubb edited Steps To Reproduce. (Show Details)
Geez changed the task status from New to Assigned.Jun 20 2022, 5:39 PM
Geez closed this task as Resolved.Apr 26 2023, 4:18 PM
Geez claimed this task.
Geez added a subscriber: Geez.

Hello TheMasterofBlubb.
According to the devs this works as designed.

"Method argument is not holding reference of object just weak pointer. Its working as designed."

TheMasterofBlubb added a comment.EditedApr 26 2023, 8:07 PM

What is the workaround for such a problem?

Because even if its just weak pointers for the parameter, it still shouldnt be cleaned mid execution of a method, even if its a thread

Edit: Because otherwise you would need a null check basically at any point where you use that variable.