Skip to content

SR-11917: Using JSONEncoder's .outputFormatting = .sortedKeys has a memory leak on Linux #2655

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
Feb 8, 2020
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
12 changes: 4 additions & 8 deletions CoreFoundation/String.subproj/CFStringUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ static UCollator *__CFStringCopyDefaultCollator(CFLocaleRef compareLocale) {
return collator;
}

#if TARGET_OS_MAC
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX
static void __collatorFinalize(UCollator *collator) {
CFLocaleRef locale = _CFGetTSD(__CFTSDKeyCollatorLocale);
_CFSetTSD(__CFTSDKeyCollatorUCollator, NULL, NULL);
Expand Down Expand Up @@ -603,28 +603,24 @@ CF_PRIVATE CFComparisonResult _CFCompareStringsWithLocale(CFStringInlineBuffer *
if (range2.location > 0) {
range2.location = __extendLocationBackward(range2.location - 1, str2, nonBaseBMP, punctBMP);
}

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX
#if TARGET_OS_MAC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exposes the variable collator at l616, which is still hidden behind an #if tag above (l577), so this doesn't build properly on other platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. Do you think the the #if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX could be removed entirely and the code would work on the BSD platforms? btw which platforms are you testing on?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that builds cleanly now.

The platform-specific branches could be removed -- I did a rush check adding TARGET_OS_BSD on every appearance of TARGET_OS_LINUX atop this commit -- but they'd need to be removed in more places than you are changing here, so perhaps that should be done in a separate PR.

However, I haven't run any of the tests; right now I am just checking to ensure things build properly on OpenBSD without error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking, yes probably best to support other platforms in a future PR.

// First we try to use the last one used on this thread, if the locale is the same,
// otherwise we try to check out a default one, or then we create one.
UCollator *threadCollator = _CFGetTSD(__CFTSDKeyCollatorUCollator);
CFLocaleRef threadLocale = _CFGetTSD(__CFTSDKeyCollatorLocale);
if (compareLocale == threadLocale) {
collator = threadCollator;
} else {
#endif
collator = __CFStringCopyDefaultCollator((CFLocaleRef)compareLocale);
defaultCollator = true;
if (NULL == collator) {
collator = __CFStringCreateCollator((CFLocaleRef)compareLocale);
defaultCollator = false;
}
#if TARGET_OS_MAC
}
#endif
#endif


characters1 = CFStringGetCharactersPtrFromInlineBuffer(str1, range1);
characters2 = CFStringGetCharactersPtrFromInlineBuffer(str2, range2);

Expand Down Expand Up @@ -739,7 +735,7 @@ CF_PRIVATE CFComparisonResult _CFCompareStringsWithLocale(CFStringInlineBuffer *
if (buffer2Len > 0) CFAllocatorDeallocate(kCFAllocatorSystemDefault, buffer2);
}

#if TARGET_OS_MAC
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX
if (collator == threadCollator) {
// do nothing, already cached
} else {
Expand Down