From 084ba6711b630173731b5191ffaee7c24b1ccc11 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Tue, 15 Dec 2020 22:09:40 -0500 Subject: [PATCH] Tests: Fix various test leaks (#2703). --- test_regress/t/TestVpi.h | 6 +++++- test_regress/t/t_dpi_import_c.cpp | 2 +- test_regress/t/t_embed1_c.cpp | 19 ++++++++++++++++++- test_regress/t/t_scope_map.cpp | 1 + test_regress/t/t_time_vpi_c.cpp | 3 ++- test_regress/t/t_trace_public_sig.cpp | 1 + test_regress/t/t_vpi_get.cpp | 2 +- test_regress/t/t_vpi_memory.cpp | 2 +- test_regress/t/t_vpi_module.cpp | 2 +- test_regress/t/t_vpi_param.cpp | 2 +- test_regress/t/t_vpi_var.cpp | 2 +- 11 files changed, 33 insertions(+), 9 deletions(-) diff --git a/test_regress/t/TestVpi.h b/test_regress/t/TestVpi.h index 843d65c4c..e6371c6c1 100644 --- a/test_regress/t/TestVpi.h +++ b/test_regress/t/TestVpi.h @@ -27,7 +27,11 @@ public: , m_free(true) {} ~TestVpiHandle() { if (m_handle && m_free) { - { vpi_free_object(m_handle); m_handle = NULL; } + // Below not VL_DO_DANGLING so is portable + { + vpi_free_object(m_handle); + m_handle = NULL; + } } } operator vpiHandle() const { return m_handle; } diff --git a/test_regress/t/t_dpi_import_c.cpp b/test_regress/t/t_dpi_import_c.cpp index 2771b70f1..c28c58d0e 100644 --- a/test_regress/t/t_dpi_import_c.cpp +++ b/test_regress/t/t_dpi_import_c.cpp @@ -124,7 +124,7 @@ void dpii_v_ushort(unsigned short i, unsigned short* o) { *o = ~i; } void dpii_v_longint(long long i, long long* o) { *o = ~i; } void dpii_v_ulong(unsigned long long i, unsigned long long* o) { *o = ~i; } void dpii_v_chandle(void* i, void** o) { *o = i; } -void dpii_v_string(const char* i, const char** o) { *o = strdup(i); } // Leaks +void dpii_v_string(const char* i, const char** o) { *o = i; } void dpii_v_real(double i, double* o) { *o = i + 1.5; } void dpii_v_shortreal(float i, float* o) { *o = i + 1.5f; } diff --git a/test_regress/t/t_embed1_c.cpp b/test_regress/t/t_embed1_c.cpp index 51ffbe375..e9deda818 100644 --- a/test_regress/t/t_embed1_c.cpp +++ b/test_regress/t/t_embed1_c.cpp @@ -61,7 +61,23 @@ Vt_embed1_child* __get_modelp() { vl_fatal(__FILE__, __LINE__, __FILE__, "svPutUserData failed"); } } - return (Vt_embed1_child*)(__modelp); + return reinterpret_cast(__modelp); +} + +void __delete_modelp() { + svScope scope = svGetScope(); + if (!scope) { + vl_fatal(__FILE__, __LINE__, __FILE__, "svGetScope failed"); + return; + } + void* __modelp = svGetUserData(scope, &T_Embed_Child_Unique); + if (__modelp) { + delete reinterpret_cast(__modelp); + __modelp = nullptr; + if (svPutUserData(scope, &T_Embed_Child_Unique, __modelp)) { + vl_fatal(__FILE__, __LINE__, __FILE__, "svPutUserData failed"); + } + } } void t_embed_child_initial() { @@ -74,6 +90,7 @@ void t_embed_child_final() { VL_DEBUG_IF(VL_PRINTF(" t_embed1_child_final\n");); Vt_embed1_child* __modelp = __get_modelp(); __modelp->final(); + __delete_modelp(); } void t_embed_child_eval() { diff --git a/test_regress/t/t_scope_map.cpp b/test_regress/t/t_scope_map.cpp index 020bee9d4..b27cb4c97 100644 --- a/test_regress/t/t_scope_map.cpp +++ b/test_regress/t/t_scope_map.cpp @@ -161,6 +161,7 @@ int main(int argc, char** argv, char** env) { tfp->close(); top->final(); + VL_DO_DANGLING(delete tfp, tfp); VL_DO_DANGLING(delete top, top); VL_PRINTF("*-* All Finished *-*\n"); diff --git a/test_regress/t/t_time_vpi_c.cpp b/test_regress/t/t_time_vpi_c.cpp index da3eb7f98..901de71cd 100644 --- a/test_regress/t/t_time_vpi_c.cpp +++ b/test_regress/t/t_time_vpi_c.cpp @@ -12,6 +12,7 @@ #include #include "svdpi.h" #include "vpi_user.h" +#include "TestVpi.h" //====================================================================== @@ -53,7 +54,7 @@ void show(vpiHandle obj) { void dpii_check() { show(0); - vpiHandle mod = vpi_handle_by_name((PLI_BYTE8*)"top.t", NULL); + TestVpiHandle mod = vpi_handle_by_name((PLI_BYTE8*)"top.t", NULL); if (!mod) { vpi_printf(const_cast("-- Cannot vpi_find module\n")); } else { diff --git a/test_regress/t/t_trace_public_sig.cpp b/test_regress/t/t_trace_public_sig.cpp index c77db8584..8cf387097 100644 --- a/test_regress/t/t_trace_public_sig.cpp +++ b/test_regress/t/t_trace_public_sig.cpp @@ -47,6 +47,7 @@ int main(int argc, char** argv, char** env) { tfp->close(); top->final(); VL_DO_DANGLING(delete top, top); + VL_DO_DANGLING(delete tfp, tfp); printf("*-* All Finished *-*\n"); return 0; } diff --git a/test_regress/t/t_vpi_get.cpp b/test_regress/t/t_vpi_get.cpp index 90741ef73..c813f8673 100644 --- a/test_regress/t/t_vpi_get.cpp +++ b/test_regress/t/t_vpi_get.cpp @@ -206,7 +206,7 @@ int mon_check() { #ifdef IS_VPI static int mon_check_vpi() { - vpiHandle href = vpi_handle(vpiSysTfCall, 0); + TestVpiHandle href = vpi_handle(vpiSysTfCall, 0); s_vpi_value vpi_value; vpi_value.format = vpiIntVal; diff --git a/test_regress/t/t_vpi_memory.cpp b/test_regress/t/t_vpi_memory.cpp index 374a166e8..6292fbe83 100644 --- a/test_regress/t/t_vpi_memory.cpp +++ b/test_regress/t/t_vpi_memory.cpp @@ -196,7 +196,7 @@ int mon_check() { #ifdef IS_VPI static int mon_check_vpi() { - vpiHandle href = vpi_handle(vpiSysTfCall, 0); + TestVpiHandle href = vpi_handle(vpiSysTfCall, 0); s_vpi_value vpi_value; vpi_value.format = vpiIntVal; diff --git a/test_regress/t/t_vpi_module.cpp b/test_regress/t/t_vpi_module.cpp index dc5929f86..74f3dc610 100644 --- a/test_regress/t/t_vpi_module.cpp +++ b/test_regress/t/t_vpi_module.cpp @@ -99,7 +99,7 @@ int mon_check() { #ifdef IS_VPI static int mon_check_vpi() { - vpiHandle href = vpi_handle(vpiSysTfCall, 0); + TestVpiHandle href = vpi_handle(vpiSysTfCall, 0); s_vpi_value vpi_value; vpi_value.format = vpiIntVal; diff --git a/test_regress/t/t_vpi_param.cpp b/test_regress/t/t_vpi_param.cpp index bfba20a21..64e54b36f 100644 --- a/test_regress/t/t_vpi_param.cpp +++ b/test_regress/t/t_vpi_param.cpp @@ -214,7 +214,7 @@ int mon_check() { #ifdef IS_VPI static int mon_check_vpi() { - vpiHandle href = vpi_handle(vpiSysTfCall, 0); + TestVpiHandle href = vpi_handle(vpiSysTfCall, 0); s_vpi_value vpi_value; vpi_value.format = vpiIntVal; diff --git a/test_regress/t/t_vpi_var.cpp b/test_regress/t/t_vpi_var.cpp index f49e4b168..e74ca8619 100644 --- a/test_regress/t/t_vpi_var.cpp +++ b/test_regress/t/t_vpi_var.cpp @@ -609,7 +609,7 @@ int mon_check() { #ifdef IS_VPI static int mon_check_vpi() { - vpiHandle href = vpi_handle(vpiSysTfCall, 0); + TestVpiHandle href = vpi_handle(vpiSysTfCall, 0); s_vpi_value vpi_value; vpi_value.format = vpiIntVal;