mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Fix: init lifecycle event payloads and clean up service listeners
- fill EventPreLoad/EventLoaded struct members instead of leaving them as uninitialized garbage; post PreLoad only after the agent environment (JavaVM/JNIEnv/JVMTI) is set so listeners can query Agent state - store (EventId, Handle) pairs in IService and add unlistenAll() to remove registered listeners on shutdown, preventing dangling callbacks into destroyed services; BytecodeService and JarLoaderService now call it from onShutdown
This commit is contained in:
1 parent
20747124d1
commit
4f433bc6ee
4 files changed
+23
-8
No files matched your search
@@ -44,10 +44,6 @@ namespace JuiceAgent {
|
||||
}
|
||||
}
|
||||
|
||||
// Post preload event
|
||||
EventPreLoad preloadEvent;
|
||||
EventBus::getInstance().post(EventId::PreLoad, &preloadEvent);
|
||||
|
||||
// Set Environment
|
||||
setJavaVM(jvm);
|
||||
setJNIEnv(env);
|
||||
@@ -60,6 +56,11 @@ namespace JuiceAgent {
|
||||
|
||||
setLoaded(true);
|
||||
|
||||
// Post preload event (after environment is set so listeners
|
||||
// can access the agent state via Agent::getInstance())
|
||||
EventPreLoad preloadEvent{ jvm, env, jvmti };
|
||||
EventBus::getInstance().post(EventId::PreLoad, &preloadEvent);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -105,7 +106,7 @@ namespace JuiceAgent {
|
||||
}
|
||||
|
||||
// Post loaded event
|
||||
EventLoaded loadedEvent;
|
||||
EventLoaded loadedEvent{ getJavaVM(), getJNIEnv(), getJVMTI() };
|
||||
EventBus::getInstance().post(EventId::Loaded, &loadedEvent);
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <eventbus.hpp>
|
||||
@@ -24,7 +25,8 @@ protected:
|
||||
static_assert(std::is_base_of<IService, ServiceT>::value,
|
||||
"ServiceT must derive from IService");
|
||||
auto* self = static_cast<ServiceT*>(this);
|
||||
handles_.push_back(
|
||||
handles_.push_back({
|
||||
id,
|
||||
EventBus::getInstance().appendListener(
|
||||
id,
|
||||
[self, handler](const void* data) {
|
||||
@@ -33,11 +35,21 @@ protected:
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Remove all listeners registered by this service. Call it in onShutdown()
|
||||
// so no callback outlives the service instance.
|
||||
void unlistenAll() {
|
||||
auto& bus = EventBus::getInstance();
|
||||
for (auto& [id, handle] : handles_) {
|
||||
bus.removeListener(id, handle);
|
||||
}
|
||||
handles_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<EventBus::Dispatcher::Handle> handles_;
|
||||
std::vector<std::pair<EventId, EventBus::Dispatcher::Handle>> handles_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public:
|
||||
}
|
||||
|
||||
void onShutdown() override {
|
||||
unlistenAll();
|
||||
spdlog::info("[BytecodeService] Shutdown");
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ public:
|
||||
}
|
||||
|
||||
void onShutdown() override {
|
||||
unlistenAll();
|
||||
spdlog::info("[JarLoaderService] Shutdown");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user