summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorritsuka <[email protected]>2015-05-30 07:45:00 +0000
committerritsuka <[email protected]>2015-05-30 07:45:00 +0000
commit39eff9d080effd0138c91f397632d24b38f7a441 (patch)
tree04e529f8b72ad44a628c6717a3b80afcc3950bb2
parent2837048a59ac5beab0b3428ca89c86857765de0a (diff)
MacGui: fix the leaks after using hb_presets_import_json and hb_presets_clean_json, add an extension to NSJSONSerialization to convert a char * directly.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@7245 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r--macosx/HBPreset.h2
-rw-r--r--macosx/HBPreset.m20
-rw-r--r--macosx/HBPresetsManager.m15
-rw-r--r--macosx/HandBrake.xcodeproj/project.pbxproj6
-rw-r--r--macosx/NSJSONSerialization+HBAdditions.h18
-rw-r--r--macosx/NSJSONSerialization+HBAdditions.m29
6 files changed, 72 insertions, 18 deletions
diff --git a/macosx/HBPreset.h b/macosx/HBPreset.h
index 10cf192d5..1c5375edb 100644
--- a/macosx/HBPreset.h
+++ b/macosx/HBPreset.h
@@ -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.
diff --git a/macosx/HBPreset.m b/macosx/HBPreset.m
index 12c9017f0..26edbba6c 100644
--- a/macosx/HBPreset.m
+++ b/macosx/HBPreset.m
@@ -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];
}
}
@@ -101,8 +102,7 @@
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]])
{
@@ -113,6 +113,8 @@
presetsArray = importedPresets;
}
}
+
+ free(importedJson);
}
if (presetsArray.count)
@@ -211,15 +213,13 @@
- (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]])
{
diff --git a/macosx/HBPresetsManager.m b/macosx/HBPresetsManager.m
index 29205e371..121473638 100644
--- a/macosx/HBPresetsManager.m
+++ b/macosx/HBPresetsManager.m
@@ -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]];
diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj
index 668962638..3026246f5 100644
--- a/macosx/HandBrake.xcodeproj/project.pbxproj
+++ b/macosx/HandBrake.xcodeproj/project.pbxproj
@@ -161,6 +161,7 @@
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 */; };
@@ -450,6 +451,8 @@
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>"; };
@@ -940,6 +943,8 @@
A952392E199A647F00588AEF /* Presets */ = {
isa = PBXGroup;
children = (
+ A99422DE1B1887B000DDB077 /* NSJSONSerialization+HBAdditions.h */,
+ A99422DF1B1887B000DDB077 /* NSJSONSerialization+HBAdditions.m */,
273F20A114ADBE670021BE6D /* HBPresetsManager.h */,
273F20A214ADBE670021BE6D /* HBPresetsManager.m */,
A9CF25F21990D64E0023F727 /* HBPreset.h */,
@@ -1378,6 +1383,7 @@
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
index 000000000..58bb47d98
--- /dev/null
+++ b/macosx/NSJSONSerialization+HBAdditions.h
@@ -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
index 000000000..0f18eb17c
--- /dev/null
+++ b/macosx/NSJSONSerialization+HBAdditions.m
@@ -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