Skip to content

Commit

Permalink
base: Make ScopedVector::clear() destroy elements.
Browse files Browse the repository at this point in the history
This makes ScopedVector's clear() method destroy the
elements before clearing the internal vector, matching the
behavior of the erase() method.  I'm moving clear()'s
previous element-preserving behavior into a new weak_clear()
method, matching weak_erase().

I'm also removing ScopedVector::reset(), as it duplicated
clear()'s new behavior and isn't a part of std::vector.

BUG=137909
TEST=added


Review URL: https://chromiumcodereview.appspot.com/10797017

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147360 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
derat@chromium.org committed Jul 19, 2012
1 parent e19d265 commit 3ebea92
Show file tree
Hide file tree
Showing 32 changed files with 63 additions and 51 deletions.
8 changes: 5 additions & 3 deletions base/memory/scoped_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ScopedVector {
const_reverse_iterator;

ScopedVector() {}
~ScopedVector() { reset(); }
~ScopedVector() { clear(); }
ScopedVector(RValue& other) { swap(other); }

ScopedVector& operator=(RValue& rhs) {
Expand Down Expand Up @@ -73,7 +73,6 @@ class ScopedVector {
v.clear();
}

void reset() { STLDeleteElements(&v); }
void reserve(size_t capacity) { v.reserve(capacity); }
void resize(size_t new_size) { v.resize(new_size); }

Expand All @@ -82,7 +81,10 @@ class ScopedVector {
v.assign(begin, end);
}

void clear() { v.clear(); }
void clear() { STLDeleteElements(&v); }

// Like |clear()|, but doesn't delete any elements.
void weak_clear() { v.clear(); }

// Lets the ScopedVector take ownership of |x|.
iterator insert(iterator position, T* x) {
Expand Down
19 changes: 16 additions & 3 deletions base/memory/scoped_vector_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,27 @@ TEST(ScopedVectorTest, LifeCycleWatcher) {
EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
}

TEST(ScopedVectorTest, Reset) {
TEST(ScopedVectorTest, Clear) {
LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject());
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
scoped_vector.reset();
scoped_vector.clear();
EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size());
}

TEST(ScopedVectorTest, WeakClear) {
LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
ScopedVector<LifeCycleObject> scoped_vector;
scoped_ptr<LifeCycleObject> object(watcher.NewLifeCycleObject());
scoped_vector.push_back(object.get());
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
scoped_vector.weak_clear();
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size());
}

TEST(ScopedVectorTest, Scope) {
Expand Down Expand Up @@ -196,7 +209,7 @@ TEST(ScopedVectorTest, Passed) {
EXPECT_EQ(0, deletes);
ScopedVector<DeleteCounter> result = callback.Run();
EXPECT_EQ(0, deletes);
result.reset();
result.clear();
EXPECT_EQ(1, deletes);
};

Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/autofill/autofill_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ void AutofillManager::UploadFormData(const FormStructure& submitted_form) {
}

void AutofillManager::Reset() {
form_structures_.reset();
form_structures_.clear();
has_logged_autofill_enabled_ = false;
has_logged_address_suggestions_count_ = false;
did_show_suggestions_ = false;
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/autofill/autofill_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ class TestPersonalDataManager : public PersonalDataManager {
}

void ClearAutofillProfiles() {
web_profiles_.reset();
web_profiles_.clear();
}

void ClearCreditCards() {
credit_cards_.reset();
credit_cards_.clear();
}

void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) {
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/autofill/autofill_merge_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand Down Expand Up @@ -89,7 +89,7 @@ PersonalDataManagerMock::~PersonalDataManagerMock() {
}

void PersonalDataManagerMock::Reset() {
profiles_.reset();
profiles_.clear();
}

void PersonalDataManagerMock::SaveImportedProfile(
Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/autofill/personal_data_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ void PersonalDataManager::SetProfiles(std::vector<AutofillProfile>* profiles) {
}

// Copy in the new profiles.
web_profiles_.reset();
web_profiles_.clear();
for (std::vector<AutofillProfile>::iterator iter = profiles->begin();
iter != profiles->end(); ++iter) {
web_profiles_.push_back(new AutofillProfile(*iter));
Expand Down Expand Up @@ -756,7 +756,7 @@ void PersonalDataManager::SetCreditCards(
}

// Copy in the new credit cards.
credit_cards_.reset();
credit_cards_.clear();
for (std::vector<CreditCard>::iterator iter = credit_cards->begin();
iter != credit_cards->end(); ++iter) {
credit_cards_.push_back(new CreditCard(*iter));
Expand Down Expand Up @@ -804,7 +804,7 @@ void PersonalDataManager::ReceiveLoadedProfiles(WebDataService::Handle h,
DCHECK_EQ(pending_profiles_query_, h);

pending_profiles_query_ = 0;
web_profiles_.reset();
web_profiles_.clear();

const WDResult<std::vector<AutofillProfile*> >* r =
static_cast<const WDResult<std::vector<AutofillProfile*> >*>(result);
Expand All @@ -824,7 +824,7 @@ void PersonalDataManager::ReceiveLoadedCreditCards(
DCHECK_EQ(pending_creditcards_query_, h);

pending_creditcards_query_ = 0;
credit_cards_.reset();
credit_cards_.clear();

const WDResult<std::vector<CreditCard*> >* r =
static_cast<const WDResult<std::vector<CreditCard*> >*>(result);
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/autofill/personal_data_manager_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void GetAddressBookPhoneNumbers(ABPerson* me,
// information and translates it to the internal list of |AutofillProfile| data
// structures.
void AuxiliaryProfilesImpl::GetAddressBookMeCard() {
profiles_.reset();
profiles_.clear();

ABAddressBook* addressBook = [ABAddressBook sharedAddressBook];
ABPerson* me = [addressBook me];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void GeolocationPermissionContextTests::SetUp() {
}

void GeolocationPermissionContextTests::TearDown() {
extra_tabs_.reset();
extra_tabs_.clear();
TabContentsTestHarness::TearDown();
// Schedule another task on the DB thread to notify us that it's safe to
// carry on with the test.
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/instant/instant_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,5 +507,5 @@ void InstantController::ScheduleDestroy(InstantLoader* loader) {
}

void InstantController::DestroyLoaders() {
loaders_to_destroy_.reset();
loaders_to_destroy_.clear();
}
6 changes: 3 additions & 3 deletions chrome/browser/net/sqlite_server_bound_cert_store_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ TEST_F(SQLiteServerBoundCertStoreTest, TestPersistence) {
store_ = NULL;
// Make sure we wait until the destructor has run.
ASSERT_TRUE(helper->Run());
certs.reset();
certs.clear();
store_ = new SQLiteServerBoundCertStore(
temp_dir_.path().Append(chrome::kOBCertFilename), NULL);

Expand Down Expand Up @@ -569,7 +569,7 @@ TEST_F(SQLiteServerBoundCertStoreTest, TestClearOnExitPolicy) {
temp_dir_.path().AppendASCII("ClearOnExitDB"), clear_policy.get());

// Reload and test for persistence
certs.reset();
certs.clear();
ASSERT_TRUE(store_->Load(&certs.get()));
ASSERT_EQ(3U, certs.size());

Expand All @@ -582,7 +582,7 @@ TEST_F(SQLiteServerBoundCertStoreTest, TestClearOnExitPolicy) {
temp_dir_.path().AppendASCII("ClearOnExitDB"), clear_policy.get());

// Reload and test for persistence
certs.reset();
certs.clear();
ASSERT_TRUE(store_->Load(&certs.get()));
ASSERT_EQ(2U, certs.size());

Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/password_manager/password_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void PasswordManager::DidNavigateAnyFrame(
// There might be password data to provisionally save. Other than that, we're
// ready to reset and move on.
ProvisionallySavePassword(params.password_form);
pending_login_managers_.reset();
pending_login_managers_.clear();
}

bool PasswordManager::OnMessageReceived(const IPC::Message& message) {
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/prerender/prerender_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ void PrerenderManager::DestroyPendingPrerenderData(
void PrerenderManager::DoShutdown() {
DestroyAllContents(FINAL_STATUS_MANAGER_SHUTDOWN);
STLDeleteElements(&prerender_conditions_);
on_close_tab_contents_deleters_.reset();
on_close_tab_contents_deleters_.clear();
profile_ = NULL;

DCHECK(active_prerender_list_.empty());
Expand Down Expand Up @@ -1231,4 +1231,3 @@ PrerenderManager* FindPrerenderManagerUsingRenderProcessId(
}

} // namespace prerender

2 changes: 1 addition & 1 deletion chrome/browser/sync/test/integration/sync_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ void SyncTest::CleanUpOnMainThread() {
// All browsers should be closed at this point, or else we could see memory
// corruption in QuitBrowser().
CHECK_EQ(0U, BrowserList::size());
clients_.reset();
clients_.clear();
}

void SyncTest::SetUpInProcessBrowserTestFixture() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ new KeywordHintDecoration(OmniboxViewMac::GetFieldFont())),
// least fail safe.
[[field_ cell] clearDecorations];

page_action_decorations_.reset();
page_action_decorations_.clear();
}

void LocationBarViewMac::RefreshPageActionDecorations() {
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/gtk/location_bar_view_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ void LocationBarViewGtk::UpdatePageActions() {
// loaded or added after startup.
if (new_page_actions != page_actions_) {
page_actions_.swap(new_page_actions);
page_action_views_.reset();
page_action_views_.clear();

for (size_t i = 0; i < page_actions_.size(); ++i) {
page_action_views_.push_back(
Expand Down Expand Up @@ -771,7 +771,7 @@ void LocationBarViewGtk::UpdatePageActions() {

void LocationBarViewGtk::InvalidatePageActions() {
size_t count_before = page_action_views_.size();
page_action_views_.reset();
page_action_views_.clear();
if (page_action_views_.size() != count_before) {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED,
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/ui/views/hung_renderer_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ RenderViewHost* HungPagesTableModel::GetRenderViewHost() {
}

void HungPagesTableModel::InitForWebContents(WebContents* hung_contents) {
tab_observers_.reset();
tab_observers_.clear();
if (hung_contents) {
// Force hung_contents to be first.
TabContents* hung_tab_contents =
Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/ui/webui/options2/password_manager_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ void PasswordManagerHandler::Observe(

void PasswordManagerHandler::UpdatePasswordLists(const ListValue* args) {
// Reset the current lists.
password_list_.reset();
password_exception_list_.reset();
password_list_.clear();
password_exception_list_.clear();

languages_ = Profile::FromWebUI(web_ui())->GetPrefs()->
GetString(prefs::kAcceptLanguages);
Expand Down Expand Up @@ -244,7 +244,7 @@ void PasswordManagerHandler::PasswordListPopulater::
const std::vector<webkit::forms::PasswordForm*>& result) {
DCHECK_EQ(pending_login_query_, handle);
pending_login_query_ = 0;
page_->password_list_.reset();
page_->password_list_.clear();
page_->password_list_.insert(page_->password_list_.end(),
result.begin(), result.end());
page_->SetPasswordList();
Expand Down Expand Up @@ -273,7 +273,7 @@ void PasswordManagerHandler::PasswordExceptionListPopulater::
const std::vector<webkit::forms::PasswordForm*>& result) {
DCHECK_EQ(pending_login_query_, handle);
pending_login_query_ = 0;
page_->password_exception_list_.reset();
page_->password_exception_list_.clear();
page_->password_exception_list_.insert(page_->password_exception_list_.end(),
result.begin(), result.end());
page_->SetPasswordExceptionList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void AutofillProfileSyncableService::StopSyncing(syncer::ModelType type) {

sync_processor_.reset();
sync_error_factory_.reset();
profiles_.reset();
profiles_.clear();
profiles_map_.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion chromeos/dbus/ibus/ibus_property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ bool CHROMEOS_EXPORT PopIBusPropertyList(dbus::MessageReader* reader,
<< "1st argument should be array.";
return false;
}
property_list->reset();
property_list->clear();
while (property_array_reader.HasMoreData()) {
IBusProperty* property = new IBusProperty;
if (!PopIBusProperty(&property_array_reader, property)) {
Expand Down
2 changes: 1 addition & 1 deletion chromeos/dbus/ibus/ibus_property_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void SetProperty(const std::string& prefix, IBusProperty* property) {
property->set_tooltip(prefix + kSampleTooltip);
property->set_visible(kSampleVisible);
property->set_checked(kSampleChecked);
property->mutable_sub_properties()->reset();
property->mutable_sub_properties()->clear();
}

// Checks testing data in |property| with |prefix|.
Expand Down
2 changes: 1 addition & 1 deletion content/browser/geolocation/location_arbitrator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void GeolocationArbitrator::DoStartProviders() {
}

void GeolocationArbitrator::StopProviders() {
providers_.reset();
providers_.clear();
}

void GeolocationArbitrator::OnAccessTokenStoresLoaded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ void RenderWidgetHostViewWin::OnDestroy() {
// sequence as part of the usual cleanup when the plugin instance goes away.
EnumChildWindows(m_hWnd, DetachPluginWindowsCallback, NULL);

props_.reset();
props_.clear();

if (base::win::GetVersion() >= base::win::VERSION_WIN7 &&
IsTouchWindow(m_hWnd, NULL)) {
Expand Down
2 changes: 1 addition & 1 deletion content/browser/speech/chunked_byte_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ scoped_ptr< std::vector<uint8> > ChunkedByteBuffer::PopChunk() {
}

void ChunkedByteBuffer::Clear() {
chunks_.reset();
chunks_.clear();
partial_chunk_.reset(new Chunk());
total_bytes_stored_ = 0;
}
Expand Down
3 changes: 1 addition & 2 deletions content/common/gpu/media/h264_dpb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ H264DPB::H264DPB() {}
H264DPB::~H264DPB() {}

void H264DPB::Clear() {
pics_.reset();
pics_.clear();
}

void H264DPB::RemoveByPOC(int poc) {
Expand Down Expand Up @@ -115,4 +115,3 @@ void H264DPB::GetLongTermRefPicsAppending(H264Picture::PtrVector& out) {
}

} // namespace content

3 changes: 1 addition & 2 deletions net/dns/dns_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ class DnsTransactionImpl : public DnsTransaction,

first_server_index_ = session_->NextFirstServerIndex();

attempts_.reset();
attempts_.clear();
return MakeAttempt();
}

Expand Down Expand Up @@ -586,4 +586,3 @@ scoped_ptr<DnsTransactionFactory> DnsTransactionFactory::CreateFactory(
}

} // namespace net

2 changes: 1 addition & 1 deletion net/proxy/dhcp_proxy_script_fetcher_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void DhcpProxyScriptFetcherWin::CancelImpl() {
(*it)->Cancel();
}

fetchers_.reset();
fetchers_.clear();
}
}

Expand Down
2 changes: 1 addition & 1 deletion ui/base/models/list_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ListModel {
// Removes all items from the model. This does NOT delete the items.
void RemoveAll() {
size_t count = item_count();
items_.clear();
items_.weak_clear();
NotifyItemsRemoved(0, count);
}

Expand Down
2 changes: 1 addition & 1 deletion ui/base/models/tree_node_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class TreeNode : public TreeModelNode {
void RemoveAll() {
for (size_t i = 0; i < children_.size(); ++i)
children_[i]->parent_ = NULL;
children_.clear();
children_.weak_clear();
}

// Returns the parent node, or NULL if this is the root node.
Expand Down
Loading

0 comments on commit 3ebea92

Please sign in to comment.