Skip to content

Commit 6868a7a

Browse files
authored
fix: memory leak on new string handling (#190)
1 parent d3ba48b commit 6868a7a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

NativeScript/runtime/ArcMacro.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* ARCMacro.h 1.2 2016/10/30 https://gist.github.com/2823399 Thanks: @olance
3+
* ARCMacro.h 1.1 2012/05/29 https://gist.github.com/2823399
4+
*
5+
* ARCMacro.h realizes coexistence of both the ARC (Automatic
6+
* Reference Counting) mode and the Non-ARC mode of Objective-C
7+
* in the same source code. This macro works for iOS and Mac OS X.
8+
*
9+
* This is a by-product of joint research by AIST and The University of Ryukyu.
10+
* HIRANO Satoshi (AIST), NAKAMURA Morikazu (U. Ryukyu) and GUAN Senlin (U. Ryukyu)
11+
*
12+
* Author: HIRANO Satoshi (AIST, Japan) on 2011/11/14
13+
* Copyright 2011-2012 National Institute of Advanced Industrial Science
14+
* and Technology (AIST), Japan. Apache License 2.0.
15+
*
16+
* Usage:
17+
* #import "ARCMacro.h"
18+
* [o1 RETAIN];
19+
* o2 = [[o3 RETAIN] AUTORELEASE];
20+
* [super DEALLOC];
21+
*/
22+
23+
#if __clang__
24+
#if __has_feature(objc_arc)
25+
#define S_RETAIN self
26+
#define S_AUTORELEASE self
27+
#define S_RELEASE self
28+
#define S_DEALLOC self
29+
#else
30+
#define S_RETAIN retain
31+
#define S_AUTORELEASE autorelease
32+
#define S_RELEASE release
33+
#define S_DEALLOC dealloc
34+
#endif
35+
#else
36+
#define S_RETAIN retain
37+
#define S_AUTORELEASE autorelease
38+
#define S_RELEASE release
39+
#define S_DEALLOC dealloc
40+
#endif

NativeScript/runtime/Helpers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include "Common.h"
77
#include "DataWrapper.h"
8+
#include "ArcMacro.h"
89

910
#ifdef __OBJC__
1011
#include <Foundation/Foundation.h>
@@ -69,7 +70,7 @@ inline std::string ToString(v8::Isolate* isolate, const v8::Local<v8::Value>& va
6970

7071
#ifdef __OBJC__
7172
inline NSString* ToNSString(const std::string& v) {
72-
return [[NSString alloc] initWithBytes:v.c_str() length:v.length() encoding:NSUTF8StringEncoding];
73+
return [[[NSString alloc] initWithBytes:v.c_str() length:v.length() encoding:NSUTF8StringEncoding] S_AUTORELEASE];
7374
}
7475
// this method is a copy of ToString to avoid needless std::string<->NSString conversions
7576
inline NSString* ToNSString(v8::Isolate* isolate, const v8::Local<v8::Value>& value) {
@@ -89,7 +90,8 @@ inline NSString* ToNSString(v8::Isolate* isolate, const v8::Local<v8::Value>& va
8990
return @"";
9091
}
9192

92-
return [[NSString alloc] initWithBytes:*result length:result.length() encoding:NSUTF8StringEncoding];
93+
return [[[NSString alloc] initWithBytes:*result length:result.length() encoding:NSUTF8StringEncoding] S_AUTORELEASE];
94+
9395
}
9496
#endif
9597
std::u16string ToUtf16String(v8::Isolate* isolate, const v8::Local<v8::Value>& value);

v8ios.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
2B7EA6AF2353477000E5184E /* NativeScriptException.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */; };
1111
2B7EA6B02353477000E5184E /* NativeScriptException.h in Headers */ = {isa = PBXBuildFile; fileRef = 2B7EA6AE2353477000E5184E /* NativeScriptException.h */; };
12+
3CBFF7442971C1C200C5DE36 /* ArcMacro.h in Headers */ = {isa = PBXBuildFile; fileRef = 3CBFF7432971C1C200C5DE36 /* ArcMacro.h */; };
1213
3CEF9CCD28F896BC0056BA45 /* SpinLock.h in Headers */ = {isa = PBXBuildFile; fileRef = 3CEF9CCC28F896B70056BA45 /* SpinLock.h */; };
1314
C205257F2577D6F900C12A5C /* NativeScript.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C2DDEB32229EAB3B00345BFE /* NativeScript.framework */; };
1415
C20525802577D6F900C12A5C /* NativeScript.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C2DDEB32229EAB3B00345BFE /* NativeScript.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@@ -542,6 +543,7 @@
542543
/* Begin PBXFileReference section */
543544
2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = NativeScriptException.mm; sourceTree = "<group>"; };
544545
2B7EA6AE2353477000E5184E /* NativeScriptException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NativeScriptException.h; sourceTree = "<group>"; };
546+
3CBFF7432971C1C200C5DE36 /* ArcMacro.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ArcMacro.h; sourceTree = "<group>"; };
545547
3CEF9CCC28F896B70056BA45 /* SpinLock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SpinLock.h; sourceTree = "<group>"; };
546548
C2003F9E23FA78CD0043B815 /* TNSDerivedClass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TNSDerivedClass.h; sourceTree = "<group>"; };
547549
C20525A72577D86600C12A5C /* TNSWidgets.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = TNSWidgets.xcframework; sourceTree = "<group>"; };
@@ -1852,6 +1854,7 @@
18521854
isa = PBXGroup;
18531855
children = (
18541856
C2DDEB66229EAC8100345BFE /* ArgConverter.h */,
1857+
3CBFF7432971C1C200C5DE36 /* ArcMacro.h */,
18551858
C2DDEB6A229EAC8200345BFE /* ArgConverter.mm */,
18561859
C2DDEB6F229EAC8200345BFE /* ArrayAdapter.h */,
18571860
C2DDEB7A229EAC8200345BFE /* ArrayAdapter.mm */,
@@ -2104,6 +2107,7 @@
21042107
C247C33A22F828E3001D2CA2 /* conversions.h in Headers */,
21052108
C247C39C22F828E3001D2CA2 /* build_config.h in Headers */,
21062109
C247C34322F828E3001D2CA2 /* remote-object-id.h in Headers */,
2110+
3CBFF7442971C1C200C5DE36 /* ArcMacro.h in Headers */,
21072111
C2DDEB96229EAC8300345BFE /* Helpers.h in Headers */,
21082112
C247C39822F828E3001D2CA2 /* vector.h in Headers */,
21092113
C247C37522F828E3001D2CA2 /* CSS.h in Headers */,

0 commit comments

Comments
 (0)