Skip to content

Commit ba78d52

Browse files
Fix for memory leak in NSURLComponents
1 parent e3f514d commit ba78d52

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

CoreFoundation/URL.subproj/CFURLComponents.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ static CFStringRef __CFURLComponentsCopyDescription(CFTypeRef cf) {
6666
return CFSTR("A really nice CFURLComponents object");
6767
}
6868

69-
static void __CFURLComponentsDeallocate(CFTypeRef cf) {
70-
CFURLComponentsRef instance = (CFURLComponentsRef)cf;
71-
__CFGenericValidateType(cf, _CFURLComponentsGetTypeID());
69+
CF_SWIFT_EXPORT void __CFURLComponentsDeallocate(CFURLComponentsRef instance) {
70+
__CFGenericValidateType(instance, _CFURLComponentsGetTypeID());
7271

7372
if (instance->_urlString) CFRelease(instance->_urlString);
7473
if (instance->_schemeComponent) CFRelease(instance->_schemeComponent);
@@ -78,6 +77,7 @@ static void __CFURLComponentsDeallocate(CFTypeRef cf) {
7877
if (instance->_pathComponent) CFRelease(instance->_pathComponent);
7978
if (instance->_queryComponent) CFRelease(instance->_queryComponent);
8079
if (instance->_fragmentComponent) CFRelease(instance->_fragmentComponent);
80+
if (instance) CFAllocatorDeallocate(kCFAllocatorSystemDefault, instance);
8181
}
8282

8383
static const CFRuntimeClass __CFURLComponentsClass = {

CoreFoundation/URL.subproj/CFURLComponents.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ CF_IMPLICIT_BRIDGING_ENABLED
2626
CF_EXTERN_C_BEGIN
2727
CF_ASSUME_NONNULL_BEGIN
2828

29+
#ifndef CF_SWIFT_EXPORT
30+
#if DEPLOYMENT_RUNTIME_SWIFT
31+
#define CF_SWIFT_EXPORT extern
32+
#else
33+
#define CF_SWIFT_EXPORT static __attribute__((used))
34+
#endif
35+
#endif
36+
2937
typedef struct __CFURLComponents *CFURLComponentsRef;
3038

3139
CF_EXPORT CFTypeID _CFURLComponentsGetTypeID(void);
3240

41+
CF_SWIFT_EXPORT void __CFURLComponentsDeallocate(CFURLComponentsRef);
42+
3343
// URLComponents are always mutable.
3444
CF_EXPORT _Nullable CFURLComponentsRef _CFURLComponentsCreate(CFAllocatorRef alloc);
3545

Foundation/NSURL.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,12 @@ open class NSURLQueryItem : NSObject, NSSecureCoding, NSCopying {
950950
open class NSURLComponents: NSObject, NSCopying {
951951
private let _components : CFURLComponentsRef!
952952

953+
deinit {
954+
if let component = _components {
955+
__CFURLComponentsDeallocate(component)
956+
}
957+
}
958+
953959
open override func copy() -> Any {
954960
return copy(with: nil)
955961
}

0 commit comments

Comments
 (0)