@@ -54,20 +54,21 @@ template <typename T> class JsiSkWrappingHostObject : public JsiSkHostObject {
54
54
55
55
/* *
56
56
* Returns the underlying object exposed by this host object. This object
57
- * should be wrapped in a shared pointer of some kind
57
+ * should be wrapped in a shared pointer of some kind.
58
+ * Throws if the object has been disposed.
58
59
* @return Underlying object
59
60
*/
60
- T getObject () { return _object ; }
61
- const T getObject () const { return _object ; }
61
+ T getObject () { return validateObject () ; }
62
+ const T getObject () const { return validateObject () ; }
62
63
63
64
/* *
64
- Updates the inner object with a new version of the object.
65
+ * Updates the inner object with a new version of the object.
65
66
*/
66
67
void setObject (T object) { _object = object; }
67
68
68
69
/* *
69
- Dispose function that can be exposed to JS by using the JSI_API_TYPENAME
70
- macro
70
+ * Dispose function that can be exposed to JS by using the JSI_API_TYPENAME
71
+ * macro.
71
72
*/
72
73
JSI_HOST_FUNCTION (dispose) {
73
74
safeDispose ();
@@ -76,12 +77,22 @@ template <typename T> class JsiSkWrappingHostObject : public JsiSkHostObject {
76
77
77
78
protected:
78
79
/* *
79
- Override to implement disposale of allocated resources like smart pointers
80
- etc. This method will only be called once for each instance of this class.
80
+ * Override to implement disposal of allocated resources like smart pointers.
81
+ * This method will only be called once for each instance of this class.
81
82
*/
82
83
virtual void releaseResources () = 0;
83
84
84
85
private:
86
+ /* *
87
+ * Validates that _object was not disposed and returns it.
88
+ */
89
+ T validateObject () const {
90
+ if (_isDisposed) {
91
+ throw std::runtime_error (" Attempted to access a disposed object." );
92
+ }
93
+ return _object;
94
+ }
95
+
85
96
void safeDispose () {
86
97
if (!_isDisposed) {
87
98
_isDisposed = true ;
@@ -90,12 +101,12 @@ template <typename T> class JsiSkWrappingHostObject : public JsiSkHostObject {
90
101
}
91
102
92
103
/* *
93
- * Wrapped object
104
+ * Wrapped object.
94
105
*/
95
106
T _object;
96
107
97
108
/* *
98
- Resource disposed flag
109
+ * Resource disposed flag.
99
110
*/
100
111
std::atomic<bool > _isDisposed = {false };
101
112
};
0 commit comments