Skip to content

[SR-3481] Fix for memory leak in NSURLComponents #899

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

Merged
merged 1 commit into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions CoreFoundation/URL.subproj/CFURLComponents.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ static CFStringRef __CFURLComponentsCopyDescription(CFTypeRef cf) {
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);
Expand All @@ -78,6 +77,7 @@ static void __CFURLComponentsDeallocate(CFTypeRef cf) {
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 = {
Expand Down
10 changes: 10 additions & 0 deletions CoreFoundation/URL.subproj/CFURLComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ CF_IMPLICIT_BRIDGING_ENABLED
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);

Expand Down
6 changes: 6 additions & 0 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,12 @@ open class NSURLQueryItem : NSObject, NSSecureCoding, NSCopying {
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)
}
Expand Down