Skip to content
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

Use block scope thread_local static for NSProgress progress stack #1912

Merged
merged 3 commits into from
Feb 7, 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
16 changes: 5 additions & 11 deletions Frameworks/Foundation/NSProgress.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,10 @@
bool childCreated;
};

// Stack of NSProgress objects that have becomeCurrent
thread_local static std::shared_ptr<std::stack<CurrentProgress>> s_currentProgressStack;

// Returns the stack of NSProgress objects that have becomeCurrent on the current thread, initializing it if necessary
static decltype(s_currentProgressStack) & _getProgressStackForCurrentThread() {
if (!s_currentProgressStack) {
s_currentProgressStack = std::make_shared<std::stack<CurrentProgress>>();
}

return s_currentProgressStack;
static auto& _getProgressStackForCurrentThread() {
thread_local static auto tlsCurrentProgressStack = std::make_shared<std::stack<CurrentProgress>>();
return tlsCurrentProgressStack;
}

@implementation NSProgress {
Expand Down Expand Up @@ -105,8 +99,8 @@ - (instancetype)initWithParent:(NSProgress*)parentProgressOrNil userInfo:(NSDict
// Assign parent
_parent = parentProgressOrNil;

// s_currentProgressStack must not be empty to have reached here
CurrentProgress& current = s_currentProgressStack->top();
// Progress stack must not be empty to have reached here
CurrentProgress& current = _getProgressStackForCurrentThread()->top();

// Keep track of the pendingUnitCount to increment in the parent
_parentPendingUnitCount = current.pendingUnitCountToAssign;
Expand Down
13 changes: 13 additions & 0 deletions tests/unittests/Foundation/NSProgressTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,16 @@ - (void)addParentTo:(NSProgress*)child {
andExpectChangeCallbacks:nil];
EXPECT_EQ(1, kvoListener.hits);
}

TEST(NSProgress, CurrentProgressLeak) {
NSProgress* progress = [NSProgress progressWithTotalUnitCount:100];

std::thread t([&progress]() {
[progress becomeCurrentWithPendingUnitCount:50];
});

t.join();

// Progress unfinished upon thread exit. Ensure we're not keeping progress alive:
EXPECT_EQ(1, [progress retainCount]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not the biggest fan of asserting on the retain count everywhere in tests.... but a conversion to ARC would be straightforward.

}