diff --git a/atom/browser/api/trackable_object.h b/atom/browser/api/trackable_object.h index d915135f0971..656904cc45d0 100644 --- a/atom/browser/api/trackable_object.h +++ b/atom/browser/api/trackable_object.h @@ -9,6 +9,7 @@ #include "atom/browser/api/event_emitter.h" #include "atom/common/id_weak_map.h" +#include "base/memory/scoped_ptr.h" namespace base { class SupportsUserData; @@ -54,7 +55,10 @@ class TrackableObject : public TrackableObjectBase { public: // Finds out the TrackableObject from its ID in weak map. static T* FromWeakMapID(v8::Isolate* isolate, int32_t id) { - v8::MaybeLocal object = weak_map_.Get(isolate, id); + if (!weak_map_) + return nullptr; + + v8::MaybeLocal object = weak_map_->Get(isolate, id); if (object.IsEmpty()) return nullptr; @@ -74,7 +78,10 @@ class TrackableObject : public TrackableObjectBase { // Returns all objects in this class's weak map. static std::vector> GetAll(v8::Isolate* isolate) { - return weak_map_.Values(isolate); + if (weak_map_) + return weak_map_->Values(isolate); + else + return std::vector>(); } TrackableObject() { @@ -83,8 +90,8 @@ class TrackableObject : public TrackableObjectBase { // Removes this instance from the weak map. void RemoveFromWeakMap() { - if (weak_map_.Has(weak_map_id())) - weak_map_.Remove(weak_map_id()); + if (weak_map_ && weak_map_->Has(weak_map_id())) + weak_map_->Remove(weak_map_id()); } protected: @@ -93,23 +100,25 @@ class TrackableObject : public TrackableObjectBase { } void AfterInit(v8::Isolate* isolate) override { - weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate)); + if (!weak_map_) + weak_map_.reset(new atom::IDWeakMap); + weak_map_id_ = weak_map_->Add(isolate, GetWrapper(isolate)); TrackableObjectBase::AfterInit(isolate); } private: // Releases all weak references in weak map, called when app is terminating. static void ReleaseAllWeakReferences() { - weak_map_.Clear(); + weak_map_.reset(); } - static atom::IDWeakMap weak_map_; + static scoped_ptr weak_map_; DISALLOW_COPY_AND_ASSIGN(TrackableObject); }; template -atom::IDWeakMap TrackableObject::weak_map_; +scoped_ptr TrackableObject::weak_map_; } // namespace mate