Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent a memory leak when converting to JSON #351

Merged
merged 1 commit into from
Jan 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions CordovaLib/Classes/Private/CDVJSON_private.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ @implementation NSArray (CDVJSONSerializingPrivate)

- (NSString*)cdv_JSONString
{
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:0
error:&error];

if (error != nil) {
NSLog(@"NSArray JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
@autoreleasepool {
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:0
error:&error];

if (error != nil) {
NSLog(@"NSArray JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
}

Expand All @@ -43,16 +45,18 @@ @implementation NSDictionary (CDVJSONSerializingPrivate)

- (NSString*)cdv_JSONString
{
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:NSJSONWritingPrettyPrinted
error:&error];

if (error != nil) {
NSLog(@"NSDictionary JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
@autoreleasepool {
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:NSJSONWritingPrettyPrinted
error:&error];

if (error != nil) {
NSLog(@"NSDictionary JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
}

Expand All @@ -62,30 +66,34 @@ @implementation NSString (CDVJSONSerializingPrivate)

- (id)cdv_JSONObject
{
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];

if (error != nil) {
NSLog(@"NSString JSONObject error: %@, Malformed Data: %@", [error localizedDescription], self);
}
@autoreleasepool {
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];

if (error != nil) {
NSLog(@"NSString JSONObject error: %@, Malformed Data: %@", [error localizedDescription], self);
}

return object;
return object;
}
}

- (id)cdv_JSONFragment
{
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingAllowFragments
error:&error];
@autoreleasepool {
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingAllowFragments
error:&error];

if (error != nil) {
NSLog(@"NSString JSONObject error: %@", [error localizedDescription]);
}
if (error != nil) {
NSLog(@"NSString JSONObject error: %@", [error localizedDescription]);
}

return object;
return object;
}
}

@end