diff --git a/source/tests/CarlaUtils.cpp b/source/tests/CarlaUtils.cpp index ba1b72f11..4dfa59990 100644 --- a/source/tests/CarlaUtils.cpp +++ b/source/tests/CarlaUtils.cpp @@ -36,7 +36,7 @@ #include "CarlaLv2Utils.hpp" #include "CarlaVstUtils.hpp" -#include "CarlaLibUtils.hpp" +#include "CarlaLibCounter.hpp" #include "CarlaShmUtils.hpp" // used in dssi utils @@ -622,6 +622,31 @@ static void test_CarlaLibUtils() noexcept const bool closed = lib_close(lib); CARLA_SAFE_ASSERT(closed); + + LibCounter lc; + void* const test1 = lc.open("/usr/lib/liblo.so"); + void* const test2 = lc.open("/usr/lib/liblo.so"); + void* const test3 = lc.open("/usr/lib/liblo.so"); + assert(test1 == test2); + assert(test2 == test3); + lc.close(test1); lc.close(test2); lc.close(test3); + + // test if the pointer changes after all closed + void* const test1b = lc.open("/usr/lib/liblo.so"); + assert(test1 != test1b); + lc.close(test1b); + + // test non-delete flag + void* const test4 = lc.open("/usr/lib/liblrdf.so.0", false); + lc.close(test4); + void* const test5 = lc.open("/usr/lib/liblrdf.so.0"); + assert(test4 == test5); + lc.close(test5); + + // open non-delete a few times, tests for cleanup on destruction + lc.open("/usr/lib/liblrdf.so.0"); + lc.open("/usr/lib/liblrdf.so.0"); + lc.open("/usr/lib/liblrdf.so.0"); } #if 0 diff --git a/source/utils/CarlaLibCounter.hpp b/source/utils/CarlaLibCounter.hpp index 7bdd21923..50721d500 100644 --- a/source/utils/CarlaLibCounter.hpp +++ b/source/utils/CarlaLibCounter.hpp @@ -38,6 +38,9 @@ public: CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0); CARLA_SAFE_ASSERT_CONTINUE(lib.lib != nullptr); + // all libs should be closed by now except those explicitly marked non-delete + CARLA_SAFE_ASSERT(! lib.canDelete); + if (! lib_close(lib.lib)) carla_stderr("LibCounter cleanup failed, reason:\n%s", lib_error(lib.filename)); @@ -65,7 +68,7 @@ public: } CARLA_SAFE_EXCEPTION_RETURN("LibCounter::open", nullptr); - const CarlaMutexLocker sl(fMutex); + const CarlaMutexLocker cml(fMutex); for (LinkedList::Itenerator it = fLibs.begin(); it.valid(); it.next()) { @@ -75,6 +78,9 @@ public: if (std::strcmp(lib.filename, filename) == 0) { + // will not be needed + delete[] dfilename; + ++lib.count; return lib.lib; } @@ -105,7 +111,7 @@ public: { CARLA_SAFE_ASSERT_RETURN(libPtr != nullptr, false); - const CarlaMutexLocker sl(fMutex); + const CarlaMutexLocker cml(fMutex); for (LinkedList::Itenerator it = fLibs.begin(); it.valid(); it.next()) {