[core] Make sure ThreadLocal will not own the pointer it is managing
ThreadLocal should not own the pointer it is managing because the use case in Mapbox GL is to keep a pointer to a stack allocated object, like: ``` MyObject foo; threadLocal.set(&foo); ``` To keep consistency, it is required that we clear the managed object before ThreadLocal gets destroyed by setting it to `nullptr`.
This commit is contained in:
@@ -29,7 +29,11 @@ ThreadLocal<T>::ThreadLocal() : impl(std::make_unique<Impl>()) {
|
||||
|
||||
template <class T>
|
||||
ThreadLocal<T>::~ThreadLocal() {
|
||||
delete get();
|
||||
// ThreadLocal will not take ownership
|
||||
// of the pointer it is managing. The pointer
|
||||
// needs to be explicitly cleared before we
|
||||
// destroy this object.
|
||||
assert(!get());
|
||||
|
||||
if (pthread_key_delete(impl->key)) {
|
||||
Log::Error(Event::General, "Failed to delete local storage key.");
|
||||
|
||||
Reference in New Issue
Block a user