[FR] Timer::callAfterDelay LambdaInvoker* or BailoutChecker

Could Timer::callAfterDelay() return a pointer to the LambdaInvoker it creates or add a 3rd parameter to pass a Component* (with a default of nullptr) so if the function’s class is destroyed before the lambda function is called it can either use BailoutChecker to see if the class exists… or in the function’s class’ destructor we could reset the LambdaInvoker.

Cheers,

Rail

Yeah, this is where lambdas start to get tricky. It’d need to be a bit more clever than just returning a pointer though, as the object it points to will be deleted, so ownership will be a problem, it’d need to return something like a WeakReference or ref-counted pointer…

        Component::SafePointer<Editor> editor(this);
		Timer::callAfterDelay(3000, [editor, this]()
		{
			if (editor != nullptr)
			{
			    // Do something
			};
		});
1 Like

Right - I figure if I had a boolean value which is set in the callback function… I could check that to know if the LambdaInvoker has called the function… and if it hasn’t set it to nullptr in the destructor…?? But I agree it’s tricky… and I’d normally just use a regular Timer… but this class where I use callAfterDelay in the ctor already is sub-classed from a class which is sub-classed from Timer… so it gets a bit tricky :slight_smile:

Cheers,

Rail

I’ll give that a shot, I was using std::bind…

Timer::callAfterDelay (5, std::bind (&MyComponent::postInit, this));

to set the function.

Thanks,

Rail

The important part is to use a a SafePointer as lambda parameter

Component::SafePointer<Editor> editor(this);

        Component::SafePointer<Editor> editor(this);
		Timer::callAfterDelay(3000, [editor, this]()
		{
			if (editor != nullptr)
			{
			     postInit();
			};
		});

Yeah - I’m trying to figure out where it’s scope is though… is editor a class variable?

Rail

no its a local, but it will be copy-by-value into the lambda, but its also a weak reference to the component you want to check

1 Like

Great! Thanks!

Rail

Unfortunately that doesn’t work with my AAX plug-in when I drag the plug-in between inserts. I’ll figure out a method using the regular Timer callback.

Cheers,

Rail

Do you call callAfterDelay from a messageThread, i think its important that that creation of the SafePointer happens on the messageThread too. Anyway, would be interesting to know why it does not work…

Yeah, it’s on the MessageThread… I’m tracking down an elusive bug in the latest Beta version of one of the major DAWs which is causing an Access Violation when instantiating my plug-in… so have to see if I can fix that first before coming back to this issue.

Thanks,

Rail