Skip to content

Commit 597804b

Browse files
committed
fix: 修复ControlUnit不能卸载的问题
fix #100
1 parent c8a5369 commit 597804b

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

source/MaaUtils/LibraryHolder/ControlUnitLibraryHolder.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bool check_version(const std::string& func_name)
1212
return false;
1313
}
1414
auto version = version_func();
15-
LogInfo << "Version:" << version;
15+
LogInfo << typeid(ControlUnitT).name() << "Library version:" << version;
1616

1717
if (std::strcmp(version, MAA_VERSION) != 0) {
1818
LogWarn << "ControlUnit and MaaFramework are not same version,"
@@ -58,7 +58,12 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> AdbControlUnitLibraryHolder::c
5858
return nullptr;
5959
}
6060

61-
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
61+
auto destroy_control_unit = [destroy_control_unit_func](MaaControlUnitHandle handle) {
62+
destroy_control_unit_func(handle);
63+
unload_library();
64+
};
65+
66+
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit);
6267
}
6368

6469
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> DbgControlUnitLibraryHolder::create_control_unit(
@@ -94,7 +99,12 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> DbgControlUnitLibraryHolder::c
9499
return nullptr;
95100
}
96101

97-
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
102+
auto destroy_control_unit = [destroy_control_unit_func](MaaControlUnitHandle handle) {
103+
destroy_control_unit_func(handle);
104+
unload_library();
105+
};
106+
107+
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit);
98108
}
99109

100110
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> ThriftControlUnitLibraryHolder::create_control_unit(
@@ -130,7 +140,12 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> ThriftControlUnitLibraryHolder
130140
return nullptr;
131141
}
132142

133-
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
143+
auto destroy_control_unit = [destroy_control_unit_func](MaaControlUnitHandle handle) {
144+
destroy_control_unit_func(handle);
145+
unload_library();
146+
};
147+
148+
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit);
134149
}
135150

136151
MAA_CTRL_NS_END

source/include/Utils/LibraryHolder.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,17 @@ template <typename T>
1616
class LibraryHolder
1717
{
1818
public:
19-
virtual ~LibraryHolder();
19+
virtual ~LibraryHolder() = default;
2020

2121
static bool load_library(const std::filesystem::path& libname);
22+
static void unload_library();
2223

2324
template <typename FuncT>
2425
static boost::function<FuncT> get_function(const std::string& func_name);
2526

2627
protected:
2728
LibraryHolder() = default;
2829

29-
private:
30-
static void unload_library();
31-
3230
private:
3331
inline static std::filesystem::path libname_;
3432
inline static int ref_count_ = 0;
@@ -37,14 +35,6 @@ class LibraryHolder
3735
inline static boost::dll::shared_library module_;
3836
};
3937

40-
template <typename T>
41-
inline LibraryHolder<T>::~LibraryHolder()
42-
{
43-
LogFunc;
44-
45-
unload_library();
46-
}
47-
4838
template <typename T>
4939
inline bool LibraryHolder<T>::load_library(const std::filesystem::path& libname)
5040
{

0 commit comments

Comments
 (0)