Manage Object Lifetime with ModalComponentManager::Callback* create (std::function<void(int)>);


#1
PopupMenu pop();
pop.showMenuAsync(PopupMenu::Options(), ModalCallbackFunction::create([this](int result) 
		{ 
			this->evaluatePopupResult(result);
		}));

If “this” is a component, what is the best way check if its still not deleted, when the lambda gets called.

Shouldn’t take showMenuAsync a second parameter (component for example), and bounds the lifetime of the lambda to the object?

Whats the best strategy?


#2

You can capture a SafePointer by value into the lambda and check it’s validity before using the this pointer.

PopupMenu m;
SafePointer<Component> sp (this);
m.showMenuAsync (PopupMenu::Options(), ModalCallbackFunction::create([sp, this] (int result) 
		{ 
			if (sp != nullptr)
                this->evaluatePopupResult (result);
		}));

Or capture only the correctly templated SafePointer and use that directly (you’ll have to make the lambda mutable though).


#3

Thank you, very clever!