Skip to content

Commit 8002b64

Browse files
committed
Use UPtr to manage DynLib lifetimes inside of BsDynLibManager.
1 parent c7e1659 commit 8002b64

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

Source/Foundation/bsfUtility/Utility/BsDynLibManager.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@
55

66
namespace bs
77
{
8-
static bool operator<(const NPtr<DynLib>& lhs, const String& rhs)
8+
9+
namespace
10+
{
11+
static void dynlib_delete(DynLib* lib)
12+
{
13+
lib->unload();
14+
bs_delete(lib);
15+
}
16+
} // namespace ()
17+
18+
static bool operator<(const UPtr<DynLib>& lhs, const String& rhs)
919
{
1020
return lhs->getName() < rhs;
1121
}
1222

13-
static bool operator<(const String& lhs, const NPtr<DynLib>& rhs)
23+
static bool operator<(const String& lhs, const UPtr<DynLib>& rhs)
1424
{
1525
return lhs < rhs->getName();
1626
}
17-
static bool operator<(const NPtr<DynLib>& lhs, const NPtr<DynLib>& rhs)
27+
28+
static bool operator<(const UPtr<DynLib>& lhs, const UPtr<DynLib>& rhs)
1829
{
1930
return lhs->getName() < rhs->getName();
2031
}
@@ -28,48 +39,38 @@ namespace bs
2839
const String::size_type length = filename.length();
2940
const String extension = String(".") + DynLib::EXTENSION;
3041
const String::size_type extLength = extension.length();
31-
if (length <= extLength || filename.substr(length - extLength) != extension)
42+
if(length <= extLength || filename.substr(length - extLength) != extension)
3243
filename.append(extension);
3344

3445
if(DynLib::PREFIX != nullptr)
3546
filename.insert(0, DynLib::PREFIX);
3647

3748
const auto& iterFind = mLoadedLibraries.lower_bound(filename);
38-
if (iterFind != mLoadedLibraries.end() && (*iterFind)->getName() == filename)
49+
if(iterFind != mLoadedLibraries.end() && (*iterFind)->getName() == filename)
3950
{
4051
return iterFind->get();
4152
}
4253
else
4354
{
4455
DynLib* newLib = bs_new<DynLib>(std::move(filename));
45-
mLoadedLibraries.emplace_hint(iterFind, newLib);
56+
mLoadedLibraries.emplace_hint(iterFind, newLib, &dynlib_delete);
4657
return newLib;
4758
}
4859
}
4960

5061
void DynLibManager::unload(DynLib* lib)
5162
{
5263
const auto& iterFind = mLoadedLibraries.find(lib->getName());
53-
if (iterFind != mLoadedLibraries.end())
64+
if(iterFind != mLoadedLibraries.end())
5465
{
5566
mLoadedLibraries.erase(iterFind);
5667
}
57-
58-
lib->unload();
59-
bs_delete(lib);
60-
}
61-
62-
DynLibManager::~DynLibManager()
63-
{
64-
// Unload & delete each resource in turn
65-
for(auto& entry : mLoadedLibraries)
68+
else
6669
{
67-
entry->unload();
68-
bs_delete(entry.get());
70+
// Somehow a DynLib not owned by the manager...?
71+
// Well, we should clean it up anyway...
72+
dynlib_delete(lib);
6973
}
70-
71-
// Empty the list
72-
mLoadedLibraries.clear();
7374
}
7475

7576
DynLibManager& gDynLibManager()

Source/Foundation/bsfUtility/Utility/BsDynLibManager.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ namespace bs
1212
*/
1313

1414
/**
15-
* This manager keeps track of all the open dynamic-loading libraries, it manages opening them opens them and can be
15+
* This manager keeps track of all the open dynamic-loading libraries, it manages opening them opens them and can be
1616
* used to lookup already already-open libraries.
1717
*
1818
* @note Not thread safe.
1919
*/
2020
class BS_UTILITY_EXPORT DynLibManager : public Module<DynLibManager>
2121
{
2222
public:
23-
DynLibManager() = default;
24-
virtual ~DynLibManager();
25-
2623
/**
2724
* Loads the given file as a dynamic library.
2825
*
@@ -34,7 +31,7 @@ namespace bs
3431
void unload(DynLib* lib);
3532

3633
protected:
37-
Set<NPtr<DynLib>, std::less<>> mLoadedLibraries;
34+
Set<UPtr<DynLib>, std::less<>> mLoadedLibraries;
3835
};
3936

4037
/** Easy way of accessing DynLibManager. */

0 commit comments

Comments
 (0)