On 2015-04-13 1:55 PM, Trevor Saunders wrote:
On Mon, Apr 13, 2015 at 01:28:05PM -0400, Ehsan Akhgari wrote:
On 2015-04-13 5:26 AM, Nicolas B. Pierron wrote:
On 04/10/2015 07:47 PM, Ehsan Akhgari wrote:
On 2015-04-10 1:41 PM, Nicolas B. Pierron wrote:
Also, what is the alternative? Acquiring a nsCOMPtr/nsRefPtr inside the
Lambda constructor (or whatever it's called)?

Yes, another option would be to ensure that the lambda cannot be used
after a specific point.

nsINode* myNode;
auto callFoo = MakeScopedLambda([&]() {
    myNode->Foo();
})
TakeLambda(callFoo);

Any reference to the lambda after the end of the innermost scope where
MakeScopedLambda is used can cause a MOZ_CRASH.

How would you detect that at compile/run time?


Simply by replacing the reference to the lambda inside callFoo at the
end of the scope, and replace it by a constructing a dummy function
which expects the same type of arguments as the lambda, but calls
MOZ_CRASH instead.

Sorry, my question was: how do you implement this with C++?  (As in, how
would an actual implementation work?)

That actually seems kind of straight forward.  You want to have an
object that wraps the provided lambda in callFoo and then nukes the
wrapping when the scope exits.  So I guess it would look something like
this (totally untested).

Does this work though? The |operator LambdaHolder| part is wrong, that operator will never be used since there is nothing which would call it when you want to pass the lambda to a different function. Also, I don't know what the master member variable is supposed to do.

If someone can come up with an implementation of this idea which actually compiles and works fine, then by all means please do file a bug and add it.

But such a class, while useful, will not actually prevent the issues that this proposal is intended to prevent (since for example in the above code, other things besides just returning from the function can still invalidate myNode.)
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to