]> granicus.if.org Git - handbrake/commitdiff
MacGui: fix the leaks after using hb_presets_import_json and hb_presets_clean_json...
authorritsuka <damiog@gmail.com>
Sat, 30 May 2015 07:45:00 +0000 (07:45 +0000)
committerritsuka <damiog@gmail.com>
Sat, 30 May 2015 07:45:00 +0000 (07:45 +0000)
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@7245 b64f7644-9d1e-0410-96f1-a4d463321fa5

macosx/HBPreset.h
macosx/HBPreset.m
macosx/HBPresetsManager.m
macosx/HandBrake.xcodeproj/project.pbxproj
macosx/NSJSONSerialization+HBAdditions.h [new file with mode: 0644]
macosx/NSJSONSerialization+HBAdditions.m [new file with mode: 0644]

index 10cf192d58d79e1b98d74b32a19f9d616c8bafd7..1c5375edb57f08d3740b09ff533f285eb94642ac 100644 (file)
@@ -35,7 +35,7 @@ typedef NS_ENUM(NSUInteger, HBPresetFormat) {
  *  @return An initialized preset—which might be different than the original receiver—that contains the preset at URL,
  *  or nil if there is an error or if the contents of the resource are not and HandBrake preset.
  */
-- (instancetype)initWithContentsOfURL:(NSURL *)url;
+- (nullable instancetype)initWithContentsOfURL:(NSURL *)url;
 
 /**
  *  Writes a property list or json representation of the contents of the preset to a given URL.
index 12c9017f06cd3a2522aa681083efe37efddb6121..26edbba6cd8ef370c9e4862cc471d8030226a5da 100644 (file)
@@ -7,6 +7,8 @@
 #import "HBPreset.h"
 #include "preset.h"
 
+#import "NSJSONSerialization+HBAdditions.h"
+
 @implementation HBPreset
 
 - (instancetype)init
@@ -74,7 +76,7 @@
     return self;
 }
 
-- (instancetype)initWithContentsOfURL:(NSURL *)url
+- (nullable instancetype)initWithContentsOfURL:(NSURL *)url
 {
     NSArray *presetsArray;
     NSString *presetsJson;
@@ -89,8 +91,7 @@
         NSArray *array = [[NSArray alloc] initWithContentsOfURL:url];
         if ([NSJSONSerialization isValidJSONObject:array])
         {
-            NSData *data = [NSJSONSerialization dataWithJSONObject:array options:0 error:NULL];
-            presetsJson = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+            presetsJson = [NSJSONSerialization HB_StringWithJSONObject:array options:0 error:NULL];
         }
     }
 
 
         if (importedJson)
         {
-            NSData *modernizedData = [NSData dataWithBytes:importedJson length:strlen(importedJson)];
-            id importedPresets = [NSJSONSerialization JSONObjectWithData:modernizedData options:0 error:NULL];
+            id importedPresets = [NSJSONSerialization HB_JSONObjectWithUTF8String:importedJson options:0 error:NULL];
 
             if ([importedPresets isKindOfClass:[NSDictionary class]])
             {
                 presetsArray = importedPresets;
             }
         }
+
+        free(importedJson);
     }
 
     if (presetsArray.count)
 - (void)cleanUp
 {
     // Run the libhb clean function
-    NSData *presetData = [NSJSONSerialization dataWithJSONObject:self.dictionary options:0 error:NULL];
-    NSString *presetJson = [[NSString alloc] initWithData:presetData encoding:NSUTF8StringEncoding];
+    NSString *presetJson = [NSJSONSerialization HB_StringWithJSONObject:self.dictionary options:0 error:NULL];
 
     if (presetJson.length)
     {
         char *cleanedJson = hb_presets_clean_json(presetJson.UTF8String);
-
-        NSData *cleanedData = [NSData dataWithBytes:cleanedJson length:strlen(cleanedJson)];
-        NSDictionary *cleanedDict = [NSJSONSerialization JSONObjectWithData:cleanedData options:0 error:NULL];
+        NSDictionary *cleanedDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:cleanedJson options:0 error:NULL];
+        free(cleanedJson);
 
         if ([cleanedDict isKindOfClass:[NSDictionary class]])
         {
index 29205e371c36d082a695f3e8e5df9ce725db65bf..121473638d841ea323e6bfca6dec61edb79836ed 100644 (file)
@@ -8,6 +8,7 @@
 #import "HBPreset.h"
 
 #import "HBUtilities.h"
+#import "NSJSONSerialization+HBAdditions.h"
 
 #include "preset.h"
 
@@ -105,18 +106,18 @@ NSString *HBPresetsChangedNotification = @"HBPresetsChangedNotification";
     else
     {
         const char *json = [[NSString alloc] initWithData:presetData encoding:NSUTF8StringEncoding].UTF8String;
-        const char *cleanedJson = hb_presets_clean_json(json);
-
-        NSData *cleanedData = [NSData dataWithBytes:cleanedJson length:strlen(cleanedJson)];
-        NSDictionary *presetsDict = [NSJSONSerialization JSONObjectWithData:cleanedData options:0 error:NULL];
+        char *cleanedJson = hb_presets_clean_json(json);
+        NSDictionary *presetsDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:cleanedJson options:0 error:NULL];
 
         if ([self checkIfOutOfDate:presetsDict])
         {
-            const char *updatedJson = hb_presets_import_json(cleanedJson);
-            NSData *updatedData = [NSData dataWithBytes:updatedJson length:strlen(cleanedJson)];
-            presetsDict = [NSJSONSerialization JSONObjectWithData:updatedData options:0 error:NULL];
+            char *updatedJson = hb_presets_import_json(cleanedJson);
+            presetsDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:updatedJson options:0 error:NULL];
+            free(updatedJson);
         }
 
+        free(cleanedJson);
+
         for (NSDictionary *child in presetsDict[@"PresetList"])
         {
             [self.root.children addObject:[[HBPreset alloc] initWithDictionary:child]];
index 66896263858dee4f785519791290ad1d13c0d451..3026246f5d54d1a2e6202b230495bd0508083848 100644 (file)
                A9906B2C1A710920001D82D5 /* HBQueueController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9906B2B1A710920001D82D5 /* HBQueueController.m */; };
                A990D9071A64562200139032 /* HBJob+HBJobConversion.m in Sources */ = {isa = PBXBuildFile; fileRef = A990D9061A64562200139032 /* HBJob+HBJobConversion.m */; };
                A9935213196F38A70069C6B7 /* ChaptersTitles.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9935211196F38A70069C6B7 /* ChaptersTitles.xib */; };
+               A99422E01B1887B000DDB077 /* NSJSONSerialization+HBAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = A99422DF1B1887B000DDB077 /* NSJSONSerialization+HBAdditions.m */; };
                A9A24B2D1B09F6FD00AD1FAB /* HBPresetsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A9A24B2C1B09F6FD00AD1FAB /* HBPresetsTests.m */; };
                A9A24B2F1B09F87400AD1FAB /* HBJobTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A9A24B2E1B09F87400AD1FAB /* HBJobTests.m */; };
                A9AA447A1970664A00D7DEFC /* HBUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = A9AA44791970664A00D7DEFC /* HBUtilities.m */; };
                A990D9051A64562200139032 /* HBJob+HBJobConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "HBJob+HBJobConversion.h"; sourceTree = "<group>"; };
                A990D9061A64562200139032 /* HBJob+HBJobConversion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "HBJob+HBJobConversion.m"; sourceTree = "<group>"; };
                A9935212196F38A70069C6B7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = ChaptersTitles.xib; sourceTree = "<group>"; };
+               A99422DE1B1887B000DDB077 /* NSJSONSerialization+HBAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSJSONSerialization+HBAdditions.h"; sourceTree = "<group>"; };
+               A99422DF1B1887B000DDB077 /* NSJSONSerialization+HBAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSJSONSerialization+HBAdditions.m"; sourceTree = "<group>"; };
                A997D8EB1A4ABB0900E19B6F /* HBPresetCoding.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HBPresetCoding.h; sourceTree = "<group>"; };
                A9A24B2C1B09F6FD00AD1FAB /* HBPresetsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBPresetsTests.m; sourceTree = "<group>"; };
                A9A24B2E1B09F87400AD1FAB /* HBJobTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBJobTests.m; sourceTree = "<group>"; };
                A952392E199A647F00588AEF /* Presets */ = {
                        isa = PBXGroup;
                        children = (
+                               A99422DE1B1887B000DDB077 /* NSJSONSerialization+HBAdditions.h */,
+                               A99422DF1B1887B000DDB077 /* NSJSONSerialization+HBAdditions.m */,
                                273F20A114ADBE670021BE6D /* HBPresetsManager.h */,
                                273F20A214ADBE670021BE6D /* HBPresetsManager.m */,
                                A9CF25F21990D64E0023F727 /* HBPreset.h */,
                                A971281F1A2C75180088C076 /* HBTitle.m in Sources */,
                                273F20B514ADBE670021BE6D /* HBPreferencesController.m in Sources */,
                                A9E66D701A67A2A8007B641D /* HBDistributedArray.m in Sources */,
+                               A99422E01B1887B000DDB077 /* NSJSONSerialization+HBAdditions.m in Sources */,
                                A9DC6C52196F04F6002AE6B4 /* HBSubtitlesController.m in Sources */,
                                A9F472891976B7F30009EC65 /* HBSubtitlesDefaultsController.m in Sources */,
                                A91AFD0C1A948827009BECED /* HBOutputFileWriter.m in Sources */,
diff --git a/macosx/NSJSONSerialization+HBAdditions.h b/macosx/NSJSONSerialization+HBAdditions.h
new file mode 100644 (file)
index 0000000..58bb47d
--- /dev/null
@@ -0,0 +1,18 @@
+/*  NSJSONSerialization+HBAdditions.h $
+
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License. */
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface NSJSONSerialization (HBAdditions)
+
++ (nullable id)HB_JSONObjectWithUTF8String:(const char *)nullTerminatedCString options:(NSJSONReadingOptions)opt error:(NSError **)error;
++ (nullable NSString *)HB_StringWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/macosx/NSJSONSerialization+HBAdditions.m b/macosx/NSJSONSerialization+HBAdditions.m
new file mode 100644 (file)
index 0000000..0f18eb1
--- /dev/null
@@ -0,0 +1,29 @@
+/*  NSJSONSerialization+HBAdditions.m $
+
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License. */
+
+#import "NSJSONSerialization+HBAdditions.h"
+
+@implementation NSJSONSerialization (HBAdditions)
+
++ (id)HB_JSONObjectWithUTF8String:(const char *)nullTerminatedCString options:(NSJSONReadingOptions)opt error:(NSError **)error;
+{
+    NSData *data = [NSData dataWithBytes:nullTerminatedCString length:strlen(nullTerminatedCString)];
+    id result = [NSJSONSerialization JSONObjectWithData:data options:opt error:error];
+    return result;
+}
+
++ (NSString *)HB_StringWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error
+{
+    NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:opt error:error];
+    if (data)
+    {
+        return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+    }
+
+    return nil;
+}
+
+@end