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:
parent
5708b7fbec
commit
7f06072420
1 changed files with 17 additions and 8 deletions
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "atom/browser/api/event_emitter.h"
|
#include "atom/browser/api/event_emitter.h"
|
||||||
#include "atom/common/id_weak_map.h"
|
#include "atom/common/id_weak_map.h"
|
||||||
|
#include "base/memory/scoped_ptr.h"
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
class SupportsUserData;
|
class SupportsUserData;
|
||||||
|
@ -54,7 +55,10 @@ class TrackableObject : public TrackableObjectBase {
|
||||||
public:
|
public:
|
||||||
// Finds out the TrackableObject from its ID in weak map.
|
// Finds out the TrackableObject from its ID in weak map.
|
||||||
static T* FromWeakMapID(v8::Isolate* isolate, int32_t id) {
|
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())
|
if (object.IsEmpty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
@ -74,7 +78,10 @@ class TrackableObject : public TrackableObjectBase {
|
||||||
|
|
||||||
// Returns all objects in this class's weak map.
|
// Returns all objects in this class's weak map.
|
||||||
static std::vector<v8::Local<v8::Object>> GetAll(v8::Isolate* isolate) {
|
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() {
|
TrackableObject() {
|
||||||
|
@ -83,8 +90,8 @@ class TrackableObject : public TrackableObjectBase {
|
||||||
|
|
||||||
// Removes this instance from the weak map.
|
// Removes this instance from the weak map.
|
||||||
void RemoveFromWeakMap() {
|
void RemoveFromWeakMap() {
|
||||||
if (weak_map_.Has(weak_map_id()))
|
if (weak_map_ && weak_map_->Has(weak_map_id()))
|
||||||
weak_map_.Remove(weak_map_id());
|
weak_map_->Remove(weak_map_id());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -93,23 +100,25 @@ class TrackableObject : public TrackableObjectBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AfterInit(v8::Isolate* isolate) override {
|
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);
|
TrackableObjectBase::AfterInit(isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Releases all weak references in weak map, called when app is terminating.
|
// Releases all weak references in weak map, called when app is terminating.
|
||||||
static void ReleaseAllWeakReferences() {
|
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);
|
DISALLOW_COPY_AND_ASSIGN(TrackableObject);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
atom::IDWeakMap TrackableObject<T>::weak_map_;
|
scoped_ptr<atom::IDWeakMap> TrackableObject<T>::weak_map_;
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue