-
Notifications
You must be signed in to change notification settings - Fork 24
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
Leak with using weak NSThread in thread #5
Comments
These shouldn't be leaked, but it's always possible there's a mistake. For reference, here's the cleanup code: If you put the above code in a loop and spin off e.g. 1000 threads, do you then get 1000 leaks? |
Yes, if you do this n times in a loop, then you get approximately n leaks. I've looked into what happens. Basically (ignoring what happens on the main thread), on the new thread we created, we create the struct TLS, destroy it, and then create it again, and never destroy it again. Here is a stack trace of each event: struct TLS created:
This is when the thread is assigned to the weak pointer. This is expected. struct TLS destroyed:
This is when the thread exits, and the struct TLS is cleaned up. This is also expected. struct TLS created again:
This is the leak. What appears to be happening is that PLWeakCompatibility is not the only one using thread-local storage. Cocoa apparently uses thread-local storage for some NSThread data, including an NSConcreteNotification. This particular thing's cleanup happens to run after PLWeakCompatibility's cleanup. However, this thing's cleanup happens to also do a -release on our thread object, which (since we used weak on threads) goes into SwizzledReleaseIMP, which tries to get the TLS (which, since we've already cleaned it up, re-creates it). The problem is that this is occurring after PLWeakCompatibility's cleanup, so it won't get cleaned up again. So it is leaked. |
Great detective work. I'll have to think about how this could potentially be fixed. In the meantime, is "don't use |
If my app use this library, will the apple reject my app? |
On profiling an app with the following code, running on iOS 4.3 simulator with PLWeakCompatibility, I am getting a leak in the Leaks instrument, saying that the struct TLS (as well as the dictionaries inside it) created in GetTLS are leaked.
Is this a bug with PLWeakCompatibility, or am I doing something wrong?
The text was updated successfully, but these errors were encountered: