diff options
author | glider@chromium.org <glider@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-04 15:21:58 +0000 |
---|---|---|
committer | glider@chromium.org <glider@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-04 15:21:58 +0000 |
commit | 6767903f48669ed860c7b0830400f469adbe556e (patch) | |
tree | 2f826e5f7d79e81607d7e145a2f1a35f9fcf2d5c /tools/xdisplaycheck | |
parent | d5d383253e80a04c5f5723605c230ea4b3d5472c (diff) | |
download | chromium_src-6767903f48669ed860c7b0830400f469adbe556e.zip chromium_src-6767903f48669ed860c7b0830400f469adbe556e.tar.gz chromium_src-6767903f48669ed860c7b0830400f469adbe556e.tar.bz2 |
Correctly release resources in xdisplaycheck.
Fix a memory leak in xdisplaycheck that causes the LeakSanitizer bot to fail.
Patch by Sergey Matveev (earthdok@chromium.org)
BUG=NONE
R=sadrul@chromium.org
Review URL: https://codereview.chromium.org/18345020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@210192 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/xdisplaycheck')
-rw-r--r-- | tools/xdisplaycheck/xdisplaycheck.cc | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/tools/xdisplaycheck/xdisplaycheck.cc b/tools/xdisplaycheck/xdisplaycheck.cc index 7bc99c5..ea5fe1e 100644 --- a/tools/xdisplaycheck/xdisplaycheck.cc +++ b/tools/xdisplaycheck/xdisplaycheck.cc @@ -34,29 +34,43 @@ void Sleep(int duration_ms) { sleep_time = remaining; } +class XScopedDisplay { + public: + XScopedDisplay() : display_(NULL) {} + ~XScopedDisplay() { + if (display_) XCloseDisplay(display_); + } + + void set(Display* display) { display_ = display; } + Display* display() { return display_; } + + private: + Display* display_; +}; + int main(int argc, char* argv[]) { - Display* display = NULL; + XScopedDisplay scoped_display; if (argv[1] && strcmp(argv[1], "--noserver") == 0) { - display = XOpenDisplay(NULL); - if (display) { + scoped_display.set(XOpenDisplay(NULL)); + if (scoped_display.display()) { fprintf(stderr, "Found unexpected connectable display %s\n", XDisplayName(NULL)); } // Return success when we got an unexpected display so that the code // without the --noserver is the same, but slow, rather than inverted. - return !display; + return !scoped_display.display(); } int kNumTries = 78; // 78*77/2 * 10 = 30s of waiting int tries; for (tries = 0; tries < kNumTries; ++tries) { - display = XOpenDisplay(NULL); - if (display) + scoped_display.set(XOpenDisplay(NULL)); + if (scoped_display.display()) break; Sleep(10 * tries); } - if (!display) { + if (!scoped_display.display()) { fprintf(stderr, "Failed to connect to %s\n", XDisplayName(NULL)); return -1; } @@ -66,14 +80,15 @@ int main(int argc, char* argv[]) { #if defined(USE_AURA) // Check for XInput2 int opcode, event, err; - if (!XQueryExtension(display, "XInputExtension", &opcode, &event, &err)) { + if (!XQueryExtension(scoped_display.display(), "XInputExtension", &opcode, + &event, &err)) { fprintf(stderr, "Failed to get XInputExtension on %s.\n", XDisplayName(NULL)); return -1; } int major = 2, minor = 0; - if (XIQueryVersion(display, &major, &minor) == BadRequest) { + if (XIQueryVersion(scoped_display.display(), &major, &minor) == BadRequest) { fprintf(stderr, "Server does not have XInput2 on %s.\n", XDisplayName(NULL)); return -1; @@ -81,13 +96,13 @@ int main(int argc, char* argv[]) { // Ask for the list of devices. This can cause some Xvfb to crash. int count = 0; - XIDeviceInfo* devices = XIQueryDevice(display, XIAllDevices, &count); + XIDeviceInfo* devices = + XIQueryDevice(scoped_display.display(), XIAllDevices, &count); if (devices) XIFreeDeviceInfo(devices); fprintf(stderr, "XInput2 verified initially sane on %s.\n", XDisplayName(NULL)); #endif - return 0; } |