]> khamidou — GitHub mirrors - electron.git/commitdiff
Fix memory leak and confirming to style guide
authorEran Tiktin <eran.t@mce-sys.com>
Thu, 20 Aug 2015 16:26:20 +0000 (19:26 +0300)
committerEran Tiktin <eran.t@mce-sys.com>
Thu, 20 Aug 2015 16:26:20 +0000 (19:26 +0300)
Fixed according to @hokein 's suggestions.

atom/common/api/atom_api_native_image.cc

index 6d15331c5e9ecc7e8cc9d8eb55f0ad843554ba66..f36e675fb9814b15578c4eb4ae71d0923abd1e28 100644 (file)
@@ -11,6 +11,7 @@
 #include "atom/common/native_mate_converters/file_path_converter.h"
 #include "atom/common/native_mate_converters/gfx_converter.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
+#include "atom/common/node_includes.h"
 #include "base/base64.h"
 #include "base/strings/string_util.h"
 #include "native_mate/dictionary.h"
 #include "ui/gfx/geometry/size.h"
 #include "ui/gfx/image/image_skia.h"
 #include "ui/gfx/image/image_util.h"
+
 #if defined(OS_WIN)
 #include "atom/common/asar/archive.h"
+#include "base/win/scoped_gdi_object.h"
 #include "ui/gfx/icon_util.h"
 #endif
 
-#include "atom/common/node_includes.h"
-
 namespace atom {
 
 namespace api {
@@ -220,41 +221,41 @@ mate::Handle<NativeImage> NativeImage::CreateFromJPEG(
 
 // static
 mate::Handle<NativeImage> NativeImage::CreateFromPath(
-    v8::Isolate* isolate, const base::FilePath& file_path) {
+    v8::Isolate* isolate, const base::FilePath& path) {
   gfx::ImageSkia image_skia;
-  base::FilePath path(file_path);
-  if (path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
+  base::FilePath image_path(path);
+  if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
 #if defined(OS_WIN)
     // If file is in asar archive, we extract it to a temp file so LoadImage can
-    // load it
+    // load it.
     base::FilePath asar_path, relative_path;
-    if (asar::GetAsarArchivePath(path, &asar_path, &relative_path)) {
+    if (asar::GetAsarArchivePath(image_path, &asar_path, &relative_path)) {
       std::shared_ptr<asar::Archive> archive =
-        asar::GetOrCreateAsarArchive(asar_path);
+          asar::GetOrCreateAsarArchive(asar_path);
       if (archive) {
-        archive->CopyFileOut(relative_path, &path);
+        archive->CopyFileOut(relative_path, &image_path);
       }
     }
 
-    HICON icon = static_cast<HICON>(LoadImage(NULL,
-      path.value().c_str(),
-      IMAGE_ICON,
-      0,
-      0,
-      LR_DEFAULTSIZE | LR_LOADFROMFILE));
+    // Load the icon from file.
+    base::win::ScopedHICON icon(
+        static_cast<HICON>(
+            LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON, 0, 0,
+                      LR_DEFAULTSIZE | LR_LOADFROMFILE)));
+
     if (icon) {
+      // Convert the icon from the Windows specific HICON to gfx::ImageSkia.
       scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon));
-      image_skia = *(new gfx::ImageSkia(gfx::ImageSkiaRep(*bitmap, 1.0f)));
-      DestroyIcon(icon);
+      image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(*bitmap, 1.0f));
     }
 #endif
   } else {
-    PopulateImageSkiaRepsFromPath(&image_skia, path);
+    PopulateImageSkiaRepsFromPath(&image_skia, image_path);
   }
   gfx::Image image(image_skia);
   mate::Handle<NativeImage> handle = Create(isolate, image);
 #if defined(OS_MACOSX)
-  if (IsTemplateFilename(path))
+  if (IsTemplateFilename(image_path))
     handle->SetTemplateImage(true);
 #endif
   return handle;