Kevin Gozali 20a8673b48 iOS: create EventTarget when creating EventEmitter and keep it until the emitter is deallocated.
Summary:
@public
There are some race conditions between VM objects getting deallocated and the instanceHandle held by the eventEmitter can point to deallocated memory space, causing undefined behavior like a crash.
For now, keep a strong ref to the eventTarget inside EventEmitter to avoid that scenario. This is a temporary workaround.

Reviewed By: shergin

Differential Revision: D8576785

fbshipit-source-id: 87ef36f716270ceca906b32bb86e0046ceaca19e
2018-06-21 14:35:39 -07:00

61 lines
1.5 KiB
C++

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <mutex>
#include <folly/dynamic.h>
#include <fabric/core/EventDispatcher.h>
#include <fabric/core/EventPrimitives.h>
#include <fabric/core/ReactPrimitives.h>
namespace facebook {
namespace react {
class EventEmitter;
using SharedEventEmitter = std::shared_ptr<const EventEmitter>;
/*
* Base class for all particular typed event handlers.
* Stores `InstanceHandle` identifying a particular component and the pointer
* to `EventDispatcher` which is responsible for delivering the event.
*
* TODO: Reconsider naming of all event-related things.
*/
class EventEmitter {
public:
EventEmitter(const InstanceHandle &instanceHandle, const Tag &tag, const SharedEventDispatcher &eventDispatcher);
virtual ~EventEmitter();
protected:
/*
* Initates an event delivery process.
* Is used by particular subclasses only.
*/
void dispatchEvent(
const std::string &type,
const folly::dynamic &payload = folly::dynamic::object(),
const EventPriority &priority = EventPriority::AsynchronousBatched
) const;
private:
EventTarget createEventTarget() const;
InstanceHandle instanceHandle_;
Tag tag_;
std::weak_ptr<const EventDispatcher> eventDispatcher_;
mutable EventTarget eventTarget_ {nullptr};
};
} // namespace react
} // namespace facebook