Avoiding creating static STL containers

It is OK under most cases, but if you happen to also use COM in your
application, you will have heap corruptions.
This commit is contained in:
Cheng Zhao 2015-07-14 17:19:46 +08:00
parent 5708b7fbec
commit 7f06072420

View file

@ -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<v8::Object> object = weak_map_.Get(isolate, id);
if (!weak_map_)
return nullptr;
v8::MaybeLocal<v8::Object> 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<v8::Local<v8::Object>> GetAll(v8::Isolate* isolate) {
return weak_map_.Values(isolate);
if (weak_map_)
return weak_map_->Values(isolate);
else
return std::vector<v8::Local<v8::Object>>();
}
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<atom::IDWeakMap> weak_map_;
DISALLOW_COPY_AND_ASSIGN(TrackableObject);
};
template<typename T>
atom::IDWeakMap TrackableObject<T>::weak_map_;
scoped_ptr<atom::IDWeakMap> TrackableObject<T>::weak_map_;
} // namespace mate