Skip to content

Commit f3d93d7

Browse files
committed
Fix Cleanup On Exit
1 parent 81fd4c9 commit f3d93d7

13 files changed

+149
-113
lines changed

RemoteInput/Echo/Atomics.cxx

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ bool atomic_lock::wait_polling(atomic_lock* self, bool (*test)(atomic_lock*), st
174174
return ::wait_polling(self, test, back_off, max_elapsed);
175175
}
176176

177+
atomic_lock::atomic_lock() : flag(false) {}
178+
177179
void atomic_lock::lock()
178180
{
179181
while (flag.test_and_set(std::memory_order_acquire))

RemoteInput/Echo/Atomics.hxx

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ private:
112112
static bool wait_polling(atomic_lock* self, bool (*test)(atomic_lock*), std::chrono::nanoseconds max_elapsed = std::chrono::nanoseconds::zero());
113113

114114
public:
115+
atomic_lock();
116+
~atomic_lock() = default;
117+
115118
void lock();
116119
void unlock();
117120

RemoteInput/Echo/Event.cxx

-5
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ Event::~Event()
111111
flag = nullptr;
112112
}
113113

114-
if (flag)
115-
{
116-
delete flag;
117-
}
118-
119114
flag = nullptr;
120115
ref = nullptr;
121116
#endif

RemoteInput/Echo/MemoryMap.cxx

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ bool MemoryMap::open(open_mode mode) noexcept
5555
return hMap != nullptr;
5656
}
5757

58-
hMap = CreateFileMappingA(hFile, nullptr, dwAccess, 0, pSize, path.c_str());
58+
hMap = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, dwAccess, 0, pSize, path.c_str());
5959
return hMap != nullptr;
6060
#else
6161
bool created = false;
@@ -275,7 +275,7 @@ bool MemoryMap::close() noexcept
275275
bool MemoryMap::is_open() const noexcept
276276
{
277277
#if defined(_WIN32) || defined(_WIN64)
278-
return (hMap != nullptr) && (hMap != INVALID_HANDLE_VALUE) && (hFile != nullptr) && (hFile != INVALID_HANDLE_VALUE);
278+
return ((hMap != nullptr) && (hMap != INVALID_HANDLE_VALUE)) || ((hFile != nullptr) && (hFile != INVALID_HANDLE_VALUE));
279279
#else
280280
return hFile != -1;
281281
#endif

RemoteInput/JVM.cxx

+26-26
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,32 @@ JVM::JVM(int argc, const char* argv[]) noexcept : vm(nullptr), createdVM(false),
7777

7878
JVM::~JVM() noexcept
7979
{
80-
// if (this->vm)
81-
// {
82-
// if (this->createdVM)
83-
// {
84-
// jint (JNICALL *pDestroyJavaVM)() = nullptr;
85-
// #if defined(_WIN32) || defined(_WIN64)
86-
// pDestroyJavaVM = reinterpret_cast<decltype(pDestroyJavaVM)>(GetProcAddress(this->module, "DestroyJavaVM"));
87-
// #else
88-
// pDestroyJavaVM = reinterpret_cast<decltype(pDestroyJavaVM)>(dlsym(this->module, "DestroyJavaVM"));
89-
// #endif // defined
90-
//
91-
// pDestroyJavaVM();
92-
// }
93-
//
94-
// this->vm = nullptr;
95-
//
96-
// if (loadedJNI)
97-
// {
98-
// #if defined(_WIN32) || defined(_WIN64)
99-
// CloseHandle(this->module);
100-
// #else
101-
// dlclose(this->module);
102-
// #endif // defined
103-
// }
104-
// this->module = nullptr;
105-
// }
80+
if (this->vm)
81+
{
82+
if (this->createdVM)
83+
{
84+
jint (JNICALL *pDestroyJavaVM)() = nullptr;
85+
#if defined(_WIN32) || defined(_WIN64)
86+
pDestroyJavaVM = reinterpret_cast<decltype(pDestroyJavaVM)>(GetProcAddress(this->module, "DestroyJavaVM"));
87+
#else
88+
pDestroyJavaVM = reinterpret_cast<decltype(pDestroyJavaVM)>(dlsym(this->module, "DestroyJavaVM"));
89+
#endif // defined
90+
91+
pDestroyJavaVM();
92+
}
93+
94+
this->vm = nullptr;
95+
96+
if (loadedJNI)
97+
{
98+
#if defined(_WIN32) || defined(_WIN64)
99+
CloseHandle(this->module);
100+
#else
101+
dlclose(this->module);
102+
#endif // defined
103+
}
104+
this->module = nullptr;
105+
}
106106
}
107107

108108
bool JVM::Init(int argc, const char* argv[]) noexcept

RemoteInput/Plugin/ControlCenter.cxx

+25-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "ReflectionHook.hxx"
99
#include "RemoteVM.hxx"
1010

11-
auto create_command_processor = [](auto processor) {
11+
auto create_command_processor = [](auto processor) -> std::thread {
1212
std::thread thread = std::thread([processor]{
1313
#if defined(_WIN32) || defined(_WIN64)
1414
HANDLE this_process = GetCurrentProcess();
@@ -30,6 +30,7 @@ auto create_command_processor = [](auto processor) {
3030
});
3131

3232
thread.detach();
33+
return thread;
3334
};
3435

3536
ControlCenter::ControlCenter(std::int32_t pid, bool is_controller, std::unique_ptr<Reflection> reflector) : pid(pid), is_controller(is_controller), stopped(is_controller), command_signal(), response_signal(), sync_signal(), main_reflector(std::move(reflector)), io_controller(), remote_vm()
@@ -61,23 +62,7 @@ ControlCenter::ControlCenter(std::int32_t pid, bool is_controller, std::unique_p
6162

6263
if (this->main_reflector)
6364
{
64-
std::thread thread = std::thread([this]{
65-
#if defined(_WIN32) || defined(_WIN64)
66-
HANDLE this_process = GetCurrentProcess();
67-
SetPriorityClass(this_process, NORMAL_PRIORITY_CLASS);
68-
69-
HANDLE this_thread = GetCurrentThread();
70-
SetThreadPriority(this_thread, THREAD_PRIORITY_HIGHEST);
71-
#else
72-
pthread_t this_thread = pthread_self();
73-
if (this_thread)
74-
{
75-
struct sched_param params = {0};
76-
params.sched_priority = sched_get_priority_max(SCHED_FIFO);
77-
pthread_setschedparam(this_thread, SCHED_FIFO, &params);
78-
}
79-
#endif // defined
80-
65+
create_command_processor([this]{
8166
if (this->main_reflector->Attach())
8267
{
8368
this->io_controller = std::make_unique<InputOutput>(this->main_reflector.get());
@@ -123,18 +108,32 @@ ControlCenter::ControlCenter(std::int32_t pid, bool is_controller, std::unique_p
123108
{
124109
this->main_reflector.reset();
125110
}
111+
112+
response_signal->signal();
126113
}
127114
});
128-
129-
thread.detach();
130115
}
131116
}
132117
}
133118

134119
ControlCenter::~ControlCenter()
135120
{
136121
terminate();
137-
//main_reflector.reset();
122+
123+
if (sync_signal)
124+
{
125+
sync_signal.reset();
126+
}
127+
128+
if (command_signal)
129+
{
130+
command_signal.reset();
131+
}
132+
133+
if (response_signal)
134+
{
135+
response_signal.reset();
136+
}
138137

139138
if (memory_map)
140139
{
@@ -158,6 +157,11 @@ void ControlCenter::terminate() noexcept
158157
{
159158
command_signal->signal();
160159
}
160+
161+
if (response_signal)
162+
{
163+
response_signal->wait();
164+
}
161165
}
162166
}
163167
}

RemoteInput/Plugin/ControlCenter.hxx

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "EIOSTypes.hxx"
1616
#include "ImageData.hxx"
1717
#include "Platform.hxx"
18-
18+
#include "Synchronization.hxx"
1919
#include "Atomics.hxx"
2020

2121
class RemoteVM;
@@ -46,9 +46,6 @@ private:
4646
template<typename Func>
4747
bool send_command(Func &&sender) const noexcept;
4848

49-
template<typename Func>
50-
bool send_command_now(Func &&sender) const noexcept;
51-
5249
void send_array_response_index_length(Stream &stream, jarray array, ReflectionType type, std::size_t index, std::size_t length) const noexcept;
5350
void send_array_response_indices(Stream &stream, jarray array, ReflectionType type, std::int32_t* indices, std::size_t length) const noexcept;
5451
void process_reflect_array_indices(Stream &stream, jarray array) const noexcept;

RemoteInput/Plugin/InputOutput.hxx

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <mutex>
1616
#include <array>
1717
#include "DetachedThreadPool.hxx"
18+
#include "ThreadPool.hxx"
1819
#include "Reflection.hxx"
1920
#include "Component.hxx"
2021
#include "RIEventQueue.hxx"
@@ -26,7 +27,7 @@ private:
2627
JavaVM* vm;
2728
jobject applet;
2829
std::mutex mutex;
29-
DetachedThreadPool input_thread;
30+
ThreadPool input_thread;
3031
std::unique_ptr<java::RIEventQueue> event_queue;
3132

3233
// MARK: - Input Variables
@@ -58,7 +59,7 @@ private:
5859
void handle_resize(java::Component* component) noexcept;
5960

6061
public:
61-
InputOutput(Reflection* reflection) noexcept;
62+
explicit InputOutput(Reflection* reflection) noexcept;
6263
~InputOutput() noexcept;
6364

6465
bool has_focus() const noexcept;

RemoteInput/Plugin/JVM/JVMCache.cxx

+17-18
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ JVMCache::JVMCache(JNIEnv* env, jobject class_loader) : class_loader(class_loade
1919

2020
JVMCache::~JVMCache()
2121
{
22-
/*for (auto&& it : class_cache)
23-
{
24-
env->DeleteGlobalRef(it.second);
25-
}*/
22+
class_cache.clear();
2623
}
2724

2825
JVMCache::JVMCache(JVMCache&& other) : class_loader(other.class_loader), load_class_method(other.load_class_method), class_cache(std::move(other.class_cache)), field_cache(std::move(other.field_cache))
@@ -53,24 +50,26 @@ jclass JVMCache::GetClass(JNIEnv* env, std::string_view name) noexcept
5350
{
5451
return it->second.get();
5552
}
56-
else
53+
54+
jstring class_name = env->NewStringUTF(name.data());
55+
if (class_name)
5756
{
58-
jstring class_name = env->NewStringUTF(name.data());
59-
if (class_name)
57+
jclass clazz = static_cast<jclass>(env->CallObjectMethod(class_loader, load_class_method, class_name, true));
58+
env->DeleteLocalRef(class_name);
59+
60+
if (clazz)
6061
{
61-
jclass clazz = static_cast<jclass>(env->CallObjectMethod(class_loader, load_class_method, class_name, true));
62-
env->DeleteLocalRef(class_name);
63-
64-
if (clazz)
65-
{
66-
env->DeleteLocalRef(std::exchange(clazz, static_cast<jclass>(env->NewGlobalRef(clazz))));
67-
class_cache.emplace(name, clazz);
68-
return clazz;
69-
}
70-
}
62+
using value_type = decltype(class_cache)::value_type::second_type;
7163

72-
return nullptr;
64+
env->DeleteLocalRef(std::exchange(clazz, static_cast<jclass>(env->NewGlobalRef(clazz))));
65+
class_cache.emplace(name, value_type(clazz, [env](jclass cls) {
66+
//env->DeleteGlobalRef(cls);
67+
}));
68+
return clazz;
69+
}
7370
}
71+
72+
return nullptr;
7473
}
7574

7675
jfieldID JVMCache::GetFieldID(JNIEnv* env, jclass clazz, std::string_view name, std::string_view sig, bool is_static) noexcept

RemoteInput/Plugin/JVM/JVMCache.hxx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private:
3434
jobject class_loader;
3535
jmethodID load_class_method;
3636

37-
std::unordered_map<std::string, std::unique_ptr<typename std::remove_pointer<jclass>::type, std::function<void(jclass)>>, string_hash, std::equal_to<>> class_cache;
37+
std::unordered_map<std::string, std::unique_ptr<std::remove_pointer<jclass>::type, std::function<void(jclass)>>, string_hash, std::equal_to<>> class_cache;
3838
std::unordered_map<std::size_t, jfieldID> field_cache;
3939

4040

0 commit comments

Comments
 (0)