diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 16:57:13 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 16:57:13 +0000 |
commit | 7c9d829192359328576965fe8a4a52a5ad6c7da3 (patch) | |
tree | 6f06ef0a1bd4f797ad2c9f2968950b625867136c /app/win | |
parent | 9d3fcf8bc99dfdfef66b983af69802ddbe76514d (diff) | |
download | chromium_src-7c9d829192359328576965fe8a4a52a5ad6c7da3.zip chromium_src-7c9d829192359328576965fe8a4a52a5ad6c7da3.tar.gz chromium_src-7c9d829192359328576965fe8a4a52a5ad6c7da3.tar.bz2 |
Attempt at fixing leaks from SetProp. Apparently there is a finite
amount of memory reserved for properties and Windows doesn't always
automatically free up the memory if the window is deleted without an
explicit remove. For the time being I've made it easier to track
SetProp leaks, but we may want to move to using something other than
SetProp in the future that isn't as fragile.
I didn't fix a couple of places that were trickier. I'm going to file
separate bugs on them.
BUG=44991
TEST=none
Review URL: http://codereview.chromium.org/4195003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64619 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/win')
-rw-r--r-- | app/win/scoped_prop.cc | 32 | ||||
-rw-r--r-- | app/win/scoped_prop.h | 40 |
2 files changed, 72 insertions, 0 deletions
diff --git a/app/win/scoped_prop.cc b/app/win/scoped_prop.cc new file mode 100644 index 0000000..e2d623e --- /dev/null +++ b/app/win/scoped_prop.cc @@ -0,0 +1,32 @@ +// Copyright (c) 2010 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. + +#include "app/win/scoped_prop.h" + +#include "base/win_util.h" + +namespace app { + +namespace win { + +ScopedProp::ScopedProp(HWND hwnd, const std::wstring& key, HANDLE data) + : hwnd_(hwnd), + key_(key) { + BOOL result = SetProp(hwnd, key.c_str(), data); + // Failure to set a propery on a window is typically fatal. It means someone + // is going to ask for the property and get NULL. So, rather than crash later + // on when someone expects a non-NULL value we crash here in hopes of + // diagnosing the failure. + CHECK(result) << win_util::FormatLastWin32Error(); +} + +ScopedProp::~ScopedProp() { + DCHECK(IsWindow(hwnd_)); + RemoveProp(hwnd_, key_.c_str()); +} + + +} // namespace win + +} // namespace app diff --git a/app/win/scoped_prop.h b/app/win/scoped_prop.h new file mode 100644 index 0000000..14e6a6a --- /dev/null +++ b/app/win/scoped_prop.h @@ -0,0 +1,40 @@ +// Copyright (c) 2010 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. + +#ifndef APP_WIN_SCOPED_PROP_H_ +#define APP_WIN_SCOPED_PROP_H_ +#pragma once + +#include <windows.h> + +#include "base/logging.h" + +namespace app { +namespace win { + +// ScopedProp is a wrapper around SetProp. Use ScopedProp rather than SetProp as +// it handles errors conditions for you and forces you to think about +// cleanup. ScopedProp must be destroyed before the window is destroyed, else +// you're going to leak a property, which could lead to failure to set a +// property later on. +class ScopedProp { + public: + // Registers the key value pair for the specified window. ScopedProp does not + // maintain the value, just the key/value pair. + ScopedProp(HWND hwnd, const std::wstring& key, HANDLE data); + ~ScopedProp(); + + const std::wstring& key() const { return key_; } + + private: + HWND hwnd_; + const std::wstring key_; + + DISALLOW_COPY_AND_ASSIGN(ScopedProp); +}; + +} // namespace win +} // namespace app + +#endif // APP_WIN_SCOPED_PROP_H_ |