Skip to content

Commit 283fce1

Browse files
xhwang-chromiumCommit Bot
authored and
Commit Bot
committed
chrome: Replace uses of ComPtr::GetAddressOf() with ComPtr::operator&
Microsoft::WRL::ComPtr<T>::GetAddressOf() is banned due to the ease of causing memory leaks. Also replace CopyTo(foo.GetAddressOf()) with As(&foo) since CopyTo(&foo) does not work if we are copying to different interfaces. Also replace QueryInterface(foo.GetAddressOf()) with QueryInterface(IID_PPV_ARGS(&foo)). Bug: 914910 Change-Id: Iab2499f6c5348afab8e3b5b7a88be7553a035dab Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2271074 Reviewed-by: Robert Liao <robliao@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Xiaohan Wang <xhwang@chromium.org> Cr-Commit-Position: refs/heads/master@{#786618}
1 parent 87e9510 commit 283fce1

18 files changed

+112
-123
lines changed

chrome/browser/download/trusted_sources_manager_win.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ TrustedSourcesManagerWin::~TrustedSourcesManagerWin() = default;
2929

3030
bool TrustedSourcesManagerWin::IsFromTrustedSource(const GURL& url) const {
3131
Microsoft::WRL::ComPtr<IInternetSecurityManager> security_manager;
32-
HRESULT hr = ::CoInternetCreateSecurityManager(
33-
NULL, security_manager.GetAddressOf(), NULL);
32+
HRESULT hr = ::CoInternetCreateSecurityManager(NULL, &security_manager, NULL);
3433
// URLZONE_LOCAL_MACHINE 0
3534
// URLZONE_INTRANET 1
3635
// URLZONE_TRUSTED 2

chrome/browser/google/google_update_win.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ HRESULT CreateGoogleUpdate3WebClass(
199199

200200
ConfigureProxyBlanket(class_factory.Get());
201201

202-
return class_factory->CreateInstance(
203-
nullptr, IID_PPV_ARGS(google_update->GetAddressOf()));
202+
return class_factory->CreateInstance(nullptr,
203+
IID_PPV_ARGS(&(*google_update)));
204204
}
205205

206206
// Returns the process-wide storage for the state of the last update check.
@@ -567,10 +567,10 @@ UpdateCheckResult UpdateCheckDriver::BeginUpdateCheckInternal() {
567567
if (!app_bundle_) {
568568
Microsoft::WRL::ComPtr<IAppBundleWeb> app_bundle;
569569
Microsoft::WRL::ComPtr<IDispatch> dispatch;
570-
hresult = google_update_->createAppBundleWeb(dispatch.GetAddressOf());
570+
hresult = google_update_->createAppBundleWeb(&dispatch);
571571
if (FAILED(hresult))
572572
return {error_code, hresult};
573-
hresult = dispatch.CopyTo(app_bundle.GetAddressOf());
573+
hresult = dispatch.As(&app_bundle);
574574
if (FAILED(hresult))
575575
return {error_code, hresult};
576576
dispatch.Reset();
@@ -614,11 +614,11 @@ UpdateCheckResult UpdateCheckDriver::BeginUpdateCheckInternal() {
614614
// this point onward result in it being released.
615615
Microsoft::WRL::ComPtr<IAppBundleWeb> app_bundle;
616616
app_bundle.Swap(app_bundle_);
617-
hresult = app_bundle->get_appWeb(0, dispatch.GetAddressOf());
617+
hresult = app_bundle->get_appWeb(0, &dispatch);
618618
if (FAILED(hresult))
619619
return {error_code, hresult};
620620
Microsoft::WRL::ComPtr<IAppWeb> app;
621-
hresult = dispatch.CopyTo(app.GetAddressOf());
621+
hresult = dispatch.As(&app);
622622
if (FAILED(hresult))
623623
return {error_code, hresult};
624624
ConfigureProxyBlanket(app.Get());
@@ -637,10 +637,10 @@ bool UpdateCheckDriver::GetCurrentState(
637637
CurrentState* state_value,
638638
HRESULT* hresult) const {
639639
Microsoft::WRL::ComPtr<IDispatch> dispatch;
640-
*hresult = app_->get_currentState(dispatch.GetAddressOf());
640+
*hresult = app_->get_currentState(&dispatch);
641641
if (FAILED(*hresult))
642642
return false;
643-
*hresult = dispatch.CopyTo(current_state->GetAddressOf());
643+
*hresult = dispatch.As(&(*current_state));
644644
if (FAILED(*hresult))
645645
return false;
646646
ConfigureProxyBlanket(current_state->Get());

chrome/browser/importer/ie_importer_browsertest_win.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ bool CreateUrlFileWithFavicon(const base::FilePath& file,
174174
if (FAILED(result))
175175
return false;
176176
Microsoft::WRL::ComPtr<IPersistFile> persist_file;
177-
result = locator.CopyTo(persist_file.GetAddressOf());
177+
result = locator.As(&persist_file);
178178
if (FAILED(result))
179179
return false;
180180
result = locator->SetURL(url.c_str(), 0);
@@ -184,11 +184,11 @@ bool CreateUrlFileWithFavicon(const base::FilePath& file,
184184
// Write favicon url if specified.
185185
if (!favicon_url.empty()) {
186186
Microsoft::WRL::ComPtr<IPropertySetStorage> property_set_storage;
187-
if (FAILED(locator.CopyTo(property_set_storage.GetAddressOf())))
187+
if (FAILED(locator.As(&property_set_storage)))
188188
return false;
189189
Microsoft::WRL::ComPtr<IPropertyStorage> property_storage;
190190
if (FAILED(property_set_storage->Open(FMTID_Intshcut, STGM_WRITE,
191-
property_storage.GetAddressOf()))) {
191+
&property_storage))) {
192192
return false;
193193
}
194194
PROPSPEC properties[] = {{PRSPEC_PROPID, {PID_IS_ICONFILE}}};

chrome/browser/media_galleries/win/mtp_device_delegate_impl_win.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ base::File::Error GetFileStreamOnBlockingPoolThread(
247247
Microsoft::WRL::ComPtr<IStream> file_stream;
248248
if (file_info.size > 0) {
249249
HRESULT hr = media_transfer_protocol::GetFileStreamForObject(
250-
device, file_object_id, file_stream.GetAddressOf(),
251-
&optimal_transfer_size);
250+
device, file_object_id, &file_stream, &optimal_transfer_size);
252251
if (hr != S_OK)
253252
return base::File::FILE_ERROR_FAILED;
254253
}

chrome/browser/media_galleries/win/mtp_device_operations_util.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ bool GetClientInformation(
3535
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
3636
base::BlockingType::MAY_BLOCK);
3737
DCHECK(client_info);
38-
HRESULT hr = ::CoCreateInstance(__uuidof(PortableDeviceValues), NULL,
39-
CLSCTX_INPROC_SERVER,
40-
IID_PPV_ARGS(client_info->GetAddressOf()));
38+
HRESULT hr =
39+
::CoCreateInstance(__uuidof(PortableDeviceValues), NULL,
40+
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&(*client_info)));
4141
if (FAILED(hr)) {
4242
DPLOG(ERROR) << "Failed to create an instance of IPortableDeviceValues";
4343
return false;
@@ -63,7 +63,7 @@ Microsoft::WRL::ComPtr<IPortableDeviceContent> GetDeviceContent(
6363
base::BlockingType::MAY_BLOCK);
6464
DCHECK(device);
6565
Microsoft::WRL::ComPtr<IPortableDeviceContent> content;
66-
if (SUCCEEDED(device->Content(content.GetAddressOf())))
66+
if (SUCCEEDED(device->Content(&content)))
6767
return content;
6868
return Microsoft::WRL::ComPtr<IPortableDeviceContent>();
6969
}
@@ -84,8 +84,8 @@ Microsoft::WRL::ComPtr<IEnumPortableDeviceObjectIDs> GetDeviceObjectEnumerator(
8484
return Microsoft::WRL::ComPtr<IEnumPortableDeviceObjectIDs>();
8585

8686
Microsoft::WRL::ComPtr<IEnumPortableDeviceObjectIDs> enum_object_ids;
87-
if (SUCCEEDED(content->EnumObjects(0, parent_id.c_str(), NULL,
88-
enum_object_ids.GetAddressOf())))
87+
if (SUCCEEDED(
88+
content->EnumObjects(0, parent_id.c_str(), NULL, &enum_object_ids)))
8989
return enum_object_ids;
9090
return Microsoft::WRL::ComPtr<IEnumPortableDeviceObjectIDs>();
9191
}
@@ -192,7 +192,7 @@ bool GetObjectDetails(IPortableDevice* device,
192192
return false;
193193

194194
Microsoft::WRL::ComPtr<IPortableDeviceProperties> properties;
195-
HRESULT hr = content->Properties(properties.GetAddressOf());
195+
HRESULT hr = content->Properties(&properties);
196196
if (FAILED(hr))
197197
return false;
198198

@@ -214,7 +214,7 @@ bool GetObjectDetails(IPortableDevice* device,
214214

215215
Microsoft::WRL::ComPtr<IPortableDeviceValues> properties_values;
216216
hr = properties->GetValues(object_id.c_str(), properties_to_read.Get(),
217-
properties_values.GetAddressOf());
217+
&properties_values);
218218
if (FAILED(hr))
219219
return false;
220220

@@ -373,7 +373,7 @@ HRESULT GetFileStreamForObject(IPortableDevice* device,
373373
return E_FAIL;
374374

375375
Microsoft::WRL::ComPtr<IPortableDeviceResources> resources;
376-
HRESULT hr = content->Transfer(resources.GetAddressOf());
376+
HRESULT hr = content->Transfer(&resources);
377377
if (FAILED(hr))
378378
return hr;
379379
return resources->GetStream(file_object_id.c_str(), WPD_RESOURCE_DEFAULT,

chrome/browser/notifications/notification_platform_bridge_win.cc

+13-15
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ typedef winfoundtn::ITypedEventHandler<
8383
ToastFailedHandler;
8484

8585
// Templated wrapper for winfoundtn::GetActivationFactory().
86-
template <unsigned int size, typename T>
87-
HRESULT CreateActivationFactory(wchar_t const (&class_name)[size], T** object) {
86+
template <unsigned int size>
87+
HRESULT CreateActivationFactory(wchar_t const (&class_name)[size],
88+
const IID& iid,
89+
void** factory) {
8890
ScopedHString ref_class_name =
8991
ScopedHString::Create(base::StringPiece16(class_name, size - 1));
90-
return base::win::RoGetActivationFactory(ref_class_name.get(),
91-
IID_PPV_ARGS(object));
92+
return base::win::RoGetActivationFactory(ref_class_name.get(), iid, factory);
9293
}
9394

9495
void ForwardNotificationOperationOnUiThread(
@@ -180,7 +181,7 @@ class NotificationPlatformBridgeWinImpl
180181
}
181182

182183
mswr::ComPtr<winxml::Dom::IXmlDocumentIO> document_io;
183-
hr = inspectable.As<winxml::Dom::IXmlDocumentIO>(&document_io);
184+
hr = inspectable.As(&document_io);
184185
if (FAILED(hr)) {
185186
LogDisplayHistogram(
186187
DisplayStatus::CONVERSION_FAILED_INSPECTABLE_TO_XML_IO);
@@ -210,7 +211,7 @@ class NotificationPlatformBridgeWinImpl
210211
toast_notification_factory;
211212
hr = CreateActivationFactory(
212213
RuntimeClass_Windows_UI_Notifications_ToastNotification,
213-
toast_notification_factory.GetAddressOf());
214+
IID_PPV_ARGS(&toast_notification_factory));
214215
if (FAILED(hr)) {
215216
LogDisplayHistogram(DisplayStatus::CREATE_FACTORY_FAILED);
216217
DLOG(ERROR) << "Unable to create the IToastNotificationFactory "
@@ -220,7 +221,7 @@ class NotificationPlatformBridgeWinImpl
220221

221222
mswr::ComPtr<winui::Notifications::IToastNotification> toast_notification;
222223
hr = toast_notification_factory->CreateToastNotification(
223-
document.Get(), toast_notification.GetAddressOf());
224+
document.Get(), &toast_notification);
224225
if (FAILED(hr)) {
225226
LogDisplayHistogram(DisplayStatus::CREATE_TOAST_NOTIFICATION_FAILED);
226227
DLOG(ERROR) << "Unable to create the IToastNotification " << std::hex
@@ -436,7 +437,7 @@ class NotificationPlatformBridgeWinImpl
436437
toast_manager;
437438
HRESULT hr = CreateActivationFactory(
438439
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
439-
toast_manager.GetAddressOf());
440+
IID_PPV_ARGS(&toast_manager));
440441
if (FAILED(hr)) {
441442
LogHistoryHistogram(
442443
HistoryStatus::CREATE_TOAST_NOTIFICATION_MANAGER_FAILED);
@@ -447,9 +448,7 @@ class NotificationPlatformBridgeWinImpl
447448

448449
mswr::ComPtr<winui::Notifications::IToastNotificationManagerStatics2>
449450
toast_manager2;
450-
hr = toast_manager
451-
.As<winui::Notifications::IToastNotificationManagerStatics2>(
452-
&toast_manager2);
451+
hr = toast_manager.As(&toast_manager2);
453452
if (FAILED(hr)) {
454453
LogHistoryHistogram(
455454
HistoryStatus::QUERY_TOAST_MANAGER_STATISTICS2_FAILED);
@@ -460,7 +459,7 @@ class NotificationPlatformBridgeWinImpl
460459

461460
mswr::ComPtr<winui::Notifications::IToastNotificationHistory>
462461
notification_history;
463-
hr = toast_manager2->get_History(notification_history.GetAddressOf());
462+
hr = toast_manager2->get_History(&notification_history);
464463
if (FAILED(hr)) {
465464
LogHistoryHistogram(HistoryStatus::GET_TOAST_HISTORY_FAILED);
466465
DLOG(ERROR) << "Failed to get IToastNotificationHistory " << std::hex
@@ -484,8 +483,7 @@ class NotificationPlatformBridgeWinImpl
484483
}
485484

486485
mswr::ComPtr<winui::Notifications::IToastNotificationHistory2> history2;
487-
HRESULT hr =
488-
history.As<winui::Notifications::IToastNotificationHistory2>(&history2);
486+
HRESULT hr = history.As(&history2);
489487
if (FAILED(hr)) {
490488
LogGetDisplayedStatus(
491489
GetDisplayedStatus::QUERY_TOAST_NOTIFICATION_HISTORY2_FAILED);
@@ -779,7 +777,7 @@ class NotificationPlatformBridgeWinImpl
779777
toast_manager;
780778
HRESULT hr = CreateActivationFactory(
781779
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
782-
toast_manager.GetAddressOf());
780+
IID_PPV_ARGS(&toast_manager));
783781
if (FAILED(hr)) {
784782
LogDisplayHistogram(
785783
DisplayStatus::CREATE_TOAST_NOTIFICATION_MANAGER_FAILED);

chrome/browser/notifications/win/fake_itoastnotification.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ HRESULT FakeIToastNotification::get_Content(winxml::Dom::IXmlDocument** value) {
2222
mswr::ComPtr<winxml::Dom::IXmlDocumentIO> xml_document_io;
2323
base::win::ScopedHString id = base::win::ScopedHString::Create(
2424
RuntimeClass_Windows_Data_Xml_Dom_XmlDocument);
25-
HRESULT hr = Windows::Foundation::ActivateInstance(
26-
id.get(), xml_document_io.GetAddressOf());
25+
HRESULT hr =
26+
Windows::Foundation::ActivateInstance(id.get(), &xml_document_io);
2727
if (FAILED(hr)) {
2828
LOG(ERROR) << "Unable to instantiate XMLDocumentIO " << hr;
2929
return hr;
@@ -37,7 +37,7 @@ HRESULT FakeIToastNotification::get_Content(winxml::Dom::IXmlDocument** value) {
3737
}
3838

3939
mswr::ComPtr<winxml::Dom::IXmlDocument> xml_document;
40-
hr = xml_document_io.CopyTo(xml_document.GetAddressOf());
40+
hr = xml_document_io.As(&xml_document);
4141
if (FAILED(hr)) {
4242
LOG(ERROR) << "Unable to copy to XMLDoc " << hr;
4343
return hr;

chrome/browser/platform_util_win.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void ShowItemInFolderOnWorkerThread(const base::FilePath& full_path) {
4646
return;
4747

4848
Microsoft::WRL::ComPtr<IShellFolder> desktop;
49-
HRESULT hr = SHGetDesktopFolder(desktop.GetAddressOf());
49+
HRESULT hr = SHGetDesktopFolder(&desktop);
5050
if (FAILED(hr))
5151
return;
5252

chrome/browser/shell_integration_win.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ int MigrateShortcutsInPathInternal(const base::FilePath& chrome_exe,
837837
Microsoft::WRL::ComPtr<IPersistFile> persist_file;
838838
if (FAILED(::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
839839
IID_PPV_ARGS(&shell_link))) ||
840-
FAILED(shell_link.CopyTo(persist_file.GetAddressOf())) ||
840+
FAILED(shell_link.As(&persist_file)) ||
841841
FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
842842
DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
843843
continue;
@@ -850,7 +850,7 @@ int MigrateShortcutsInPathInternal(const base::FilePath& chrome_exe,
850850
// Validate the existing app id for the shortcut.
851851
Microsoft::WRL::ComPtr<IPropertyStore> property_store;
852852
propvariant.Reset();
853-
if (FAILED(shell_link.CopyTo(property_store.GetAddressOf())) ||
853+
if (FAILED(shell_link.As(&property_store)) ||
854854
property_store->GetValue(PKEY_AppUserModel_ID, propvariant.Receive()) !=
855855
S_OK) {
856856
// When in doubt, prefer not updating the shortcut.

chrome/browser/ui/views/accessibility/navigation_accessibility_uitest_win.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ void WinAccessibilityEventMonitor::WaitForNextEvent(
122122

123123
Microsoft::WRL::ComPtr<IAccessible> acc_obj;
124124
base::win::ScopedVariant child_variant;
125-
CHECK(S_OK == AccessibleObjectFromEvent(
126-
event_info.hwnd, event_info.obj_id, event_info.child_id,
127-
acc_obj.GetAddressOf(), child_variant.Receive()));
125+
CHECK(S_OK == AccessibleObjectFromEvent(event_info.hwnd, event_info.obj_id,
126+
event_info.child_id, &acc_obj,
127+
child_variant.Receive()));
128128

129129
base::win::ScopedVariant role_variant;
130130
if (S_OK == acc_obj->get_accRole(child_variant, role_variant.Receive()))

chrome/browser/ui/views/status_icons/status_tray_state_changer_win.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ bool StatusTrayStateChangerWin::CreateTrayNotify() {
116116
return false;
117117

118118
Microsoft::WRL::ComPtr<ITrayNotifyWin8> tray_notify_win8;
119-
hr = tray_notify_.CopyTo(tray_notify_win8.GetAddressOf());
119+
hr = tray_notify_.As(&tray_notify_win8);
120120
if (SUCCEEDED(hr)) {
121121
interface_version_ = INTERFACE_VERSION_WIN8;
122122
return true;
123123
}
124124

125125
Microsoft::WRL::ComPtr<ITrayNotify> tray_notify_legacy;
126-
hr = tray_notify_.CopyTo(tray_notify_legacy.GetAddressOf());
126+
hr = tray_notify_.As(&tray_notify_legacy);
127127
if (SUCCEEDED(hr)) {
128128
interface_version_ = INTERFACE_VERSION_LEGACY;
129129
return true;
@@ -158,7 +158,7 @@ std::unique_ptr<NOTIFYITEM> StatusTrayStateChangerWin::RegisterCallback() {
158158

159159
bool StatusTrayStateChangerWin::RegisterCallbackWin8() {
160160
Microsoft::WRL::ComPtr<ITrayNotifyWin8> tray_notify_win8;
161-
HRESULT hr = tray_notify_.CopyTo(tray_notify_win8.GetAddressOf());
161+
HRESULT hr = tray_notify_.As(&tray_notify_win8);
162162
if (FAILED(hr))
163163
return false;
164164

@@ -178,7 +178,7 @@ bool StatusTrayStateChangerWin::RegisterCallbackWin8() {
178178

179179
bool StatusTrayStateChangerWin::RegisterCallbackLegacy() {
180180
Microsoft::WRL::ComPtr<ITrayNotify> tray_notify;
181-
HRESULT hr = tray_notify_.CopyTo(tray_notify.GetAddressOf());
181+
HRESULT hr = tray_notify_.As(&tray_notify);
182182
if (FAILED(hr)) {
183183
return false;
184184
}
@@ -205,12 +205,12 @@ void StatusTrayStateChangerWin::SendNotifyItemUpdate(
205205
std::unique_ptr<NOTIFYITEM> notify_item) {
206206
if (interface_version_ == INTERFACE_VERSION_LEGACY) {
207207
Microsoft::WRL::ComPtr<ITrayNotify> tray_notify;
208-
HRESULT hr = tray_notify_.CopyTo(tray_notify.GetAddressOf());
208+
HRESULT hr = tray_notify_.As(&tray_notify);
209209
if (SUCCEEDED(hr))
210210
tray_notify->SetPreference(notify_item.get());
211211
} else if (interface_version_ == INTERFACE_VERSION_WIN8) {
212212
Microsoft::WRL::ComPtr<ITrayNotifyWin8> tray_notify;
213-
HRESULT hr = tray_notify_.CopyTo(tray_notify.GetAddressOf());
213+
HRESULT hr = tray_notify_.As(&tray_notify);
214214
if (SUCCEEDED(hr))
215215
tray_notify->SetPreference(notify_item.get());
216216
}

0 commit comments

Comments
 (0)