@@ -19,10 +19,6 @@ PluginObject::PluginObject(std::string m_name, ContextKinds m_kind)
1919
2020PluginObject::~PluginObject ()
2121{
22- if (eventFunctionPtr)
23- {
24- delete eventFunctionPtr;
25- }
2622}
2723
2824void PluginObject::RegisterEventHandlerJSON (EValue* functionPtr)
@@ -57,9 +53,6 @@ void PluginObject::UnregisterEventHandling(std::string eventName)
5753
5854EventResult PluginObject::TriggerEvent (std::string invokedBy, std::string eventName, std::vector<std::any> eventPayload, ClassData* eventObject)
5955{
60- if (GetPluginState () == PluginState_t::Stopped && eventName != " OnPluginStart" && eventName != " OnAllPluginsLoaded" )
61- return EventResult::Continue;
62-
6356 if (!eventFunctionPtr)
6457 return EventResult::Continue;
6558
@@ -73,11 +66,14 @@ EventResult PluginObject::TriggerEvent(std::string invokedBy, std::string eventN
7366 EventResult response = EventResult::Continue;
7467 try
7568 {
76- if (!eventObject) {
77- ClassData tmpObject ({ { " plugin_name" , invokedBy } }, " Event" , ctx);
78- eventObject = &tmpObject;
69+ ClassData* localObj = eventObject;
70+ bool created = false ;
71+ if (!localObj) {
72+ localObj = new ClassData ({ { " plugin_name" , invokedBy } }, " Event" , ctx);
73+ created = true ;
7974 }
80- auto value = (*eventFunctionPtr)(eventObject, eventName, eventPayload);
75+
76+ auto value = (*eventFunctionPtr)(localObj, eventName, eventPayload);
8177 if (value.isNumber ())
8278 {
8379 int result = value.cast <int >();
@@ -86,6 +82,7 @@ EventResult PluginObject::TriggerEvent(std::string invokedBy, std::string eventN
8682 else
8783 response = (EventResult)result;
8884 }
85+ if (created) delete localObj;
8986 }
9087 catch (EException& e)
9188 {
@@ -101,9 +98,6 @@ EventResult PluginObject::TriggerEvent(std::string invokedBy, std::string eventN
10198
10299EventResult PluginObject::TriggerEventJSON (std::string invokedBy, std::string eventName, std::string eventPayload, ClassData* eventObject)
103100{
104- if (GetPluginState () == PluginState_t::Stopped && eventName != " OnPluginStart" && eventName != " OnAllPluginsLoaded" )
105- return EventResult::Continue;
106-
107101 if (!eventFunctionPtr)
108102 return EventResult::Continue;
109103
@@ -117,11 +111,14 @@ EventResult PluginObject::TriggerEventJSON(std::string invokedBy, std::string ev
117111 EventResult response = EventResult::Continue;
118112 try
119113 {
120- if (!eventObject) {
121- ClassData tmpObject ({ { " plugin_name" , invokedBy } }, " Event" , ctx);
122- eventObject = &tmpObject;
114+ ClassData* localObj = eventObject;
115+ bool created = false ;
116+ if (!localObj) {
117+ localObj = new ClassData ({ { " plugin_name" , invokedBy } }, " Event" , ctx);
118+ created = true ;
123119 }
124- auto value = (*eventFunctionPtrJSON)(eventObject, eventName, eventPayload);
120+
121+ auto value = (*eventFunctionPtrJSON)(localObj, eventName, eventPayload);
125122 if (value.isNumber ())
126123 {
127124 int result = value.cast <int >();
@@ -130,6 +127,7 @@ EventResult PluginObject::TriggerEventJSON(std::string invokedBy, std::string ev
130127 else
131128 response = (EventResult)result;
132129 }
130+ if (created) delete localObj;
133131 }
134132 catch (EException& e)
135133 {
@@ -304,6 +302,17 @@ void PluginObject::DestroyScriptingEnvironment()
304302 g_commandsManager.UnregisterCommand (command);
305303
306304 eventHandlers.clear ();
305+
306+ if (eventFunctionPtr) {
307+ delete eventFunctionPtr;
308+ eventFunctionPtr = nullptr ;
309+ }
310+
311+ if (eventFunctionPtrJSON) {
312+ delete eventFunctionPtrJSON;
313+ eventFunctionPtrJSON = nullptr ;
314+ }
315+
307316 delete ctx;
308317}
309318
0 commit comments