New issue
Advanced search Search tips

Issue 648382 link

Starred by 2 users

Issue metadata

Status: Archived
Owner: ----
Closed: Sep 13
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: All
Pri: 3
Type: Feature



Sign in to add a comment

Simplify Views ownership model

Project Member Reported by pkasting@chromium.org, Sep 19 2016

Issue description

The Views ownership model is complicated.  Usually, Views are owned by their parents, and AddChildView() is thus effectively an ownership transfer.  But this ownership transfer is implicit, both at the callsite and in the View itself (it holds children as raw pointers, not unique_ptrs).

This is confusing and error-prone.  Today, caller code typically looks like:

  View* blah = new View(blah);
  ... use |blah|
  parent_->AddChildView(blah);

It's not obvious here that AddChildView() transfers ownership and thus avoids a leak.  It's easy to introduce a leak with an early-return or other such change.  The code uses a bare new, which ideally I'd like to avoid long-term.

We could sort-of improve today's world by simply changing callers to something like:

  auto blah = base::MakeUnique<View>(blah);
  ... use |blah|
  parent_->AddChildView(blah.release());

...but this is more verbose than the current code, and the type system doesn't force you to do this, so it's still easy to avoid doing it.

Ideally, we should be in a world where we have something more like:

  // view.h:
  View* AddChildView(std::unique_ptr<View> child);

  // Caller:
  my_blah_ = parent_->AddChildView(base::MakeUnique<View>(blah));
  ... use |my_blah_|

This makes the ownership transfer clear.  More importantly, it makes the API difficult to misuse.

This isn't as simple as it sounds, because sometimes Views are owned elsewhere.  The set_owned_by_client() method toggles this state on.

So we first need to try and remove set_owned_by_client(), so Views are always owned by their parents.  This may not be easy, as doing this requires understanding precisely what the ownership model is for the existing callsites and whether/how it could be modeled differently.

Then we can change to use unique_ptrs for View's child pointers and the APIs that use them.

CCing some possibly-interested parties.
 
Cc: tfarina@chromium.org
Project Member

Comment 2 by sheriffbot@chromium.org, Oct 13 2017

Labels: Hotlist-Recharge-Cold
Status: Untriaged (was: Available)
This issue has been Available for over a year. If it's no longer important or seems unlikely to be fixed, please consider closing it out. If it is important, please re-triage the issue.

Sorry for the inconvenience if the bug really should have been left as Available. If you change it back, also remove the "Hotlist-Recharge-Cold" label.

For more details visit https://www.chromium.org/issue-tracking/autotriage - Your friendly Sheriffbot
Status: Available (was: Untriaged)
Yep, we still want to do this.
Status: Archived (was: Available)
Archiving old bugs that haven't been actively assigned in over 180 days.

If you feel this issue should still be addressed, feel free to reopen it or to file a new issue. Thanks!

Sign in to add a comment