Bug 1588241 Comment 39 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

(In reply to Jean-Yves Avenard [:jya] from comment #38)
> :ehsan ; the static analyser will always complain whenever you are capturing a pointer to a refcounted object by value.
> 
> There are legitimate cases on which you would want to do so; like the ChannelEventQueue ; it guarantees that the object in the queue will not outlive its owner.
> 
> Could we add a way to bypass this error? it makes for frustrating time sometimes, as we now need to deal with reference cycles that shouldn't exist in the first place.

Do you mean something effectively like `UnsafePtr` in https://phabricator.services.mozilla.com/D52261 for https://hg.mozilla.org/integration/autoland/rev/8d2cf0fc6b9b#l1.35?

(How come the version of the patch that landed there was different than the one in Phabricator BTW?!)

If I understand what you want to achieve here correctly, then yes I think that should be easily possible.  We could do so by adding an attribute hidden behind a macro that would allow you to write the code like this:

```
    template <typename T>
   NeckoTargetChannelFunctionEvent(T* aChild, std::function<void()>&& aCallback)
       : ChannelFunctionEvent(
             [MOZ_UNCHECKED child = aChild]() {
               MOZ_ASSERT(child);
               return child->GetNeckoTarget();
            },
            std::move(aCallback)) {}
```

Or something to that effect.  That way usage of refcounted pointers with lambdas is safe by default and if you know the lifetime of your objects precisely and would like to opt out of the compiler-enforced safety checks you could do so and make it clear to the reader of the code that the specific lambda capture in question isn't being checked.

If this sounds good please file a bug and CC Andi, he should be able to help you out.  :-)
(In reply to Jean-Yves Avenard [:jya] from comment #38)
> :ehsan ; the static analyser will always complain whenever you are capturing a pointer to a refcounted object by value.
> 
> There are legitimate cases on which you would want to do so; like the ChannelEventQueue ; it guarantees that the object in the queue will not outlive its owner.
> 
> Could we add a way to bypass this error? it makes for frustrating time sometimes, as we now need to deal with reference cycles that shouldn't exist in the first place.

Do you mean something effectively like `UnsafePtr` in https://phabricator.services.mozilla.com/D52261 for https://hg.mozilla.org/integration/autoland/rev/8d2cf0fc6b9b#l1.35?

(How come the version of the patch that landed there was different than the one in Phabricator BTW?!)

If I understand what you want to achieve here correctly, then yes I think that should be easily possible.  We could do so by adding an attribute hidden behind a macro that would allow you to write the code like this:

```
   template <typename T>
   NeckoTargetChannelFunctionEvent(T* aChild, std::function<void()>&& aCallback)
       : ChannelFunctionEvent(
            [MOZ_UNCHECKED child = aChild]() {
               MOZ_ASSERT(child);
               return child->GetNeckoTarget();
            },
            std::move(aCallback)) {}
```

Or something to that effect.  That way usage of refcounted pointers with lambdas is safe by default and if you know the lifetime of your objects precisely and would like to opt out of the compiler-enforced safety checks you could do so and make it clear to the reader of the code that the specific lambda capture in question isn't being checked.

If this sounds good please file a bug and CC Andi, he should be able to help you out.  :-)

Back to Bug 1588241 Comment 39