Fix for memory leak in NSURLComponents (#899)
diff --git a/CoreFoundation/URL.subproj/CFURLComponents.c b/CoreFoundation/URL.subproj/CFURLComponents.c
index 1ff7bf5..c1732e4 100644
--- a/CoreFoundation/URL.subproj/CFURLComponents.c
+++ b/CoreFoundation/URL.subproj/CFURLComponents.c
@@ -66,9 +66,8 @@
return CFSTR("A really nice CFURLComponents object");
}
-static void __CFURLComponentsDeallocate(CFTypeRef cf) {
- CFURLComponentsRef instance = (CFURLComponentsRef)cf;
- __CFGenericValidateType(cf, _CFURLComponentsGetTypeID());
+CF_SWIFT_EXPORT void __CFURLComponentsDeallocate(CFURLComponentsRef instance) {
+ __CFGenericValidateType(instance, _CFURLComponentsGetTypeID());
if (instance->_urlString) CFRelease(instance->_urlString);
if (instance->_schemeComponent) CFRelease(instance->_schemeComponent);
@@ -78,6 +77,7 @@
if (instance->_pathComponent) CFRelease(instance->_pathComponent);
if (instance->_queryComponent) CFRelease(instance->_queryComponent);
if (instance->_fragmentComponent) CFRelease(instance->_fragmentComponent);
+ if (instance) CFAllocatorDeallocate(kCFAllocatorSystemDefault, instance);
}
static const CFRuntimeClass __CFURLComponentsClass = {
diff --git a/CoreFoundation/URL.subproj/CFURLComponents.h b/CoreFoundation/URL.subproj/CFURLComponents.h
index 014ef00..095f285 100644
--- a/CoreFoundation/URL.subproj/CFURLComponents.h
+++ b/CoreFoundation/URL.subproj/CFURLComponents.h
@@ -26,10 +26,20 @@
CF_EXTERN_C_BEGIN
CF_ASSUME_NONNULL_BEGIN
+#ifndef CF_SWIFT_EXPORT
+#if DEPLOYMENT_RUNTIME_SWIFT
+#define CF_SWIFT_EXPORT extern
+#else
+#define CF_SWIFT_EXPORT static __attribute__((used))
+#endif
+#endif
+
typedef struct __CFURLComponents *CFURLComponentsRef;
CF_EXPORT CFTypeID _CFURLComponentsGetTypeID(void);
+CF_SWIFT_EXPORT void __CFURLComponentsDeallocate(CFURLComponentsRef);
+
// URLComponents are always mutable.
CF_EXPORT _Nullable CFURLComponentsRef _CFURLComponentsCreate(CFAllocatorRef alloc);
diff --git a/Foundation/NSURL.swift b/Foundation/NSURL.swift
index d6f3207..4616022 100644
--- a/Foundation/NSURL.swift
+++ b/Foundation/NSURL.swift
@@ -956,6 +956,12 @@
open class NSURLComponents: NSObject, NSCopying {
private let _components : CFURLComponentsRef!
+ deinit {
+ if let component = _components {
+ __CFURLComponentsDeallocate(component)
+ }
+ }
+
open override func copy() -> Any {
return copy(with: nil)
}