Skip to content

Commit

Permalink
fix: ensure persistence store still exists when GC runs (#21417)
Browse files Browse the repository at this point in the history
Fix a bad access crash that happens when a render frame is deleted (window closed) and garbage collection runs afterward.
  • Loading branch information
trop[bot] authored and MarshallOfSound committed Dec 6, 2019
1 parent d0e4bd3 commit 6156254
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
public:
static void BindTo(v8::Isolate* isolate,
v8::Local<v8::Object> target,
RenderFramePersistenceStore* store,
base::WeakPtr<RenderFramePersistenceStore> store,
WeakGlobalPairNode* node,
int hash) {
new CachedProxyLifeMonitor(isolate, target, store, node, hash);
Expand All @@ -29,7 +29,7 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
protected:
CachedProxyLifeMonitor(v8::Isolate* isolate,
v8::Local<v8::Object> target,
RenderFramePersistenceStore* store,
base::WeakPtr<RenderFramePersistenceStore> store,
WeakGlobalPairNode* node,
int hash)
: ObjectLifeMonitor(isolate, target),
Expand All @@ -38,6 +38,9 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
hash_(hash) {}

void RunDestructor() override {
if (!store_)
return;

if (node_->detached) {
delete node_;
return;
Expand All @@ -56,7 +59,7 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
}

private:
RenderFramePersistenceStore* store_;
base::WeakPtr<RenderFramePersistenceStore> store_;
WeakGlobalPairNode* node_;
int hash_;
};
Expand Down Expand Up @@ -98,11 +101,11 @@ void RenderFramePersistenceStore::CacheProxiedObject(
auto iter = proxy_map_.find(hash);
auto* node = new WeakGlobalPairNode(
std::make_tuple(std::move(global_from), std::move(global_proxy)));
CachedProxyLifeMonitor::BindTo(v8::Isolate::GetCurrent(), obj, this, node,
hash);
CachedProxyLifeMonitor::BindTo(v8::Isolate::GetCurrent(), obj,
weak_factory_.GetWeakPtr(), node, hash);
CachedProxyLifeMonitor::BindTo(v8::Isolate::GetCurrent(),
v8::Local<v8::Object>::Cast(proxy_value),
this, node, hash);
weak_factory_.GetWeakPtr(), node, hash);
if (iter == proxy_map_.end()) {
proxy_map_.emplace(hash, node);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class RenderFramePersistenceStore final : public content::RenderFrameObserver {

// object_identity ==> [from_value, proxy_value]
std::map<int, WeakGlobalPairNode*> proxy_map_;
base::WeakPtrFactory<RenderFramePersistenceStore> weak_factory_{this};
};

} // namespace context_bridge
Expand Down

0 comments on commit 6156254

Please sign in to comment.