From 86d7984e25843eabdb5c7230761f7461653d222a Mon Sep 17 00:00:00 2001 From: Kondapally Kalyan Date: Sat, 17 Aug 2013 16:14:10 +0300 Subject: [PATCH 1/3] Wrong buffer size in DefaultDisplaySpec We are using sizeof(pointer) as size of the buffer. This is wrong as size of pointer is not equal to size of array it points to. Size of pointer is usually 4 bytes on 32 bit system or size of the address register on the particular machine controls the size of the pointer. In this case we already know the size as we are allocating the char array on heap. This patch makes necessary changes in this direction. --- surface_factory_wayland.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/surface_factory_wayland.cc b/surface_factory_wayland.cc index 9a129207..3e250a96 100644 --- a/surface_factory_wayland.cc +++ b/surface_factory_wayland.cc @@ -122,9 +122,9 @@ gfx::AcceleratedWidget SurfaceFactoryWayland::RealizeAcceleratedWidget( const char* SurfaceFactoryWayland::DefaultDisplaySpec() { std::list screens = display_->GetScreenList(); gfx::Rect scrn = screens.front()->GetAllocation(); - char *str = new char[12]; - - base::snprintf(str, sizeof(str), "%dx%d", scrn.width(), scrn.height()); + int size = 12; + char *str = new char[size]; + base::snprintf(str, size, "%dx%d", scrn.width(), scrn.height()); return str; } From c7dc733a28de253b3f9f13d5368a935db9e1e9bd Mon Sep 17 00:00:00 2001 From: Kondapally Kalyan Date: Sat, 17 Aug 2013 18:39:44 +0300 Subject: [PATCH 2/3] Fix memory leak in DefaultDisplaySpec. We allocate a new array on heap on every call to DefaultDisplaySpec but don't destroy it anywhere. This patch fixes this by allocating the array once and destroying it when surface_factory_wayland is destroyed. --- surface_factory_wayland.cc | 20 ++++++++++++++------ surface_factory_wayland.h | 1 + 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/surface_factory_wayland.cc b/surface_factory_wayland.cc index 3e250a96..a450d144 100644 --- a/surface_factory_wayland.cc +++ b/surface_factory_wayland.cc @@ -83,11 +83,15 @@ void SurfaceFactoryWayland::InitializeWaylandEvent() SurfaceFactoryWayland::SurfaceFactoryWayland() : e_factory(NULL), - display_(ui::WaylandDisplay::GetDisplay()) { + display_(ui::WaylandDisplay::GetDisplay()), + spec_(0 ){ LOG(INFO) << "Ozone: SurfaceFactoryWayland"; } -SurfaceFactoryWayland::~SurfaceFactoryWayland() { +SurfaceFactoryWayland::~SurfaceFactoryWayland() +{ + if (spec_) + delete spec_; } intptr_t SurfaceFactoryWayland::InitializeHardware() @@ -120,13 +124,17 @@ gfx::AcceleratedWidget SurfaceFactoryWayland::RealizeAcceleratedWidget( } const char* SurfaceFactoryWayland::DefaultDisplaySpec() { + // (kalyan) We could track active mode of front screen + // instead of having to go through the modes on every call. std::list screens = display_->GetScreenList(); gfx::Rect scrn = screens.front()->GetAllocation(); - int size = 12; - char *str = new char[size]; - base::snprintf(str, size, "%dx%d", scrn.width(), scrn.height()); + int size = 2 * sizeof scrn.width(); + if (!spec_) + spec_ = new char[size]; + + base::snprintf(spec_, size, "%dx%d", scrn.width(), scrn.height()); - return str; + return spec_; } gfx::Screen* SurfaceFactoryWayland::CreateDesktopScreen() { diff --git a/surface_factory_wayland.h b/surface_factory_wayland.h index 8d924ebc..f6db6cd5 100644 --- a/surface_factory_wayland.h +++ b/surface_factory_wayland.h @@ -36,6 +36,7 @@ class SurfaceFactoryWayland : public SurfaceFactoryOzone { ui::EventFactoryWayland *e_factory; ui::WaylandDisplay* display_; + char* spec_; }; } // namespace ui From 18df4da7ca2cd59bd3b51d038a5a940593bf07a1 Mon Sep 17 00:00:00 2001 From: Kondapally Kalyan Date: Sat, 17 Aug 2013 18:43:59 +0300 Subject: [PATCH 3/3] fix un-necessary space --- surface_factory_wayland.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/surface_factory_wayland.cc b/surface_factory_wayland.cc index a450d144..dd519a31 100644 --- a/surface_factory_wayland.cc +++ b/surface_factory_wayland.cc @@ -84,7 +84,7 @@ void SurfaceFactoryWayland::InitializeWaylandEvent() SurfaceFactoryWayland::SurfaceFactoryWayland() : e_factory(NULL), display_(ui::WaylandDisplay::GetDisplay()), - spec_(0 ){ + spec_(0){ LOG(INFO) << "Ozone: SurfaceFactoryWayland"; }