diff --git a/src/evhelper.h b/src/evhelper.h index 95f874a..7b4f8be 100644 --- a/src/evhelper.h +++ b/src/evhelper.h @@ -75,6 +75,7 @@ struct PVXS_API evbase { void assertInLoop(); bool inLoop(); + INST_COUNTER(evbase); private: struct Pvt; std::unique_ptr pvt; diff --git a/src/pvxs/unittest.h b/src/pvxs/unittest.h index b2a450c..8b9c412 100644 --- a/src/pvxs/unittest.h +++ b/src/pvxs/unittest.h @@ -22,6 +22,21 @@ namespace pvxs { +/** Free some internal global allocations to avoid false positives in + * valgrind (or similar) tools looking for memory leaks. + * + * Calls libevent_global_shutdown() when available (libevent >=2.1). + * + * @warning This function is optional. + * If you don't understand the intended use case, then do not call it! + * + * @pre Caller must release all resources explicitly allocated through PVXS (on all threads). + * @post Invalidates internal state. + * Use of __any__ API functions afterwards is undefined! + */ +PVXS_API +void cleanup_for_valgrind(); + /** A single test case (or diagnostic line). * * Acts as an output string to accumulate test comment. diff --git a/src/pvxs/version.h b/src/pvxs/version.h index 85527aa..85a9874 100644 --- a/src/pvxs/version.h +++ b/src/pvxs/version.h @@ -53,21 +53,6 @@ const char *version_str(); PVXS_API unsigned long version_int(); -/** Free some internal global allocations to avoid false positives in - * valgrind (or similar) tools looking for memory leaks. - * - * Calls libevent_global_shutdown() when available (libevent >=2.1). - * - * @warning This function is optional. - * If you don't understand the intended use case, then do not call it! - * - * @pre Caller must release all resources explicitly allocated through PVXS (on all threads). - * @post Invalidates internal state. - * Use of __any__ API functions afterwards is undefined! - */ -PVXS_API -void cleanup_for_valgrind(); - } #endif // PVXS_VERSION_H diff --git a/src/unittest.cpp b/src/unittest.cpp index 909fe89..3e21d88 100644 --- a/src/unittest.cpp +++ b/src/unittest.cpp @@ -8,9 +8,25 @@ #include "pvxs/unittest.h" #include "utilpvt.h" +#include "udp_collector.h" namespace pvxs { +void cleanup_for_valgrind() +{ + for(auto& pair : instanceSnapshot()) { + // This will mess up test counts, but is the only way + // 'prove' will print the result in CI runs. + if(pair.second!=0) + testFail("Instance leak %s : %zu", pair.first.c_str(), pair.second); + } +#if LIBEVENT_VERSION_NUMBER >= 0x02010000 + libevent_global_shutdown(); +#endif + impl::logger_shutdown(); + impl::UDPManager::cleanup(); +} + testCase::testCase() :result(Diag) {} diff --git a/src/util.cpp b/src/util.cpp index 72dc68c..1137860 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -49,21 +49,13 @@ unsigned long version_int() return PVXS_VERSION; } -void cleanup_for_valgrind() -{ -#if LIBEVENT_VERSION_NUMBER >= 0x02010000 - libevent_global_shutdown(); -#endif - impl::logger_shutdown(); - impl::UDPManager::cleanup(); -} - #define CASE(KLASS) std::atomic cnt_ ## KLASS{} CASE(StructTop); CASE(UDPListener); +CASE(evbase); CASE(GPROp); CASE(Connection); @@ -99,6 +91,7 @@ std::map instanceSnapshot() CASE(StructTop); CASE(UDPListener); +CASE(evbase); CASE(GPROp); CASE(Connection); diff --git a/src/utilpvt.h b/src/utilpvt.h index abfb671..e50d213 100644 --- a/src/utilpvt.h +++ b/src/utilpvt.h @@ -256,6 +256,7 @@ struct InstCounter CASE(StructTop); CASE(UDPListener); +CASE(evbase); CASE(GPROp); CASE(Connection); diff --git a/test/testev.cpp b/test/testev.cpp index 4cf2c36..977bc00 100644 --- a/test/testev.cpp +++ b/test/testev.cpp @@ -26,6 +26,9 @@ void test_call() evbase base("TEST"); + auto snap = instanceSnapshot(); + testEq(snap["evbase"], 1u); + testOk1(!base.inLoop()); { @@ -113,7 +116,7 @@ void test_fill_evbuf() MAIN(testev) { SockAttach attach; - testPlan(14); + testPlan(15); test_call(); test_fill_evbuf(); cleanup_for_valgrind();