From 8c14a7f35aede1bedb2cab02fbb235e38efb4e13 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Tue, 26 Sep 2023 16:07:02 +0200 Subject: [PATCH 1/4] Fix leak in `nrn_thread_free`. The array of `Prop*` is allocated unconditionally in `thread_memblist_setup`. tml->ml->prop = new Prop*[mlcnt[i]]; // used for ode_map if (!memb_func[i].hoc_mech) { CACHELINE_ALLOC(tml->ml->pdata, Datum*, mlcnt[i]); } Hence this commit adjust the logic in `nrn_thread_free` deallocate `ml->prop` unconditionally. --- src/nrnoc/multicore.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/nrnoc/multicore.cpp b/src/nrnoc/multicore.cpp index efe6d3d077..2de1d26d2a 100644 --- a/src/nrnoc/multicore.cpp +++ b/src/nrnoc/multicore.cpp @@ -393,10 +393,8 @@ void nrn_threads_free() { tml2 = tml->next; free((char*) ml->nodelist); free((char*) ml->nodeindices); - if (memb_func[tml->index].hoc_mech) { - free((char*) ml->prop); - } else { - // free((char*) ml->_data); + free((char*) ml->prop); + if (!memb_func[tml->index].hoc_mech) { free((char*) ml->pdata); } if (ml->_thread) { From 692a72457389d1fd5a2cebd77554fcde5bec04ab Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Wed, 27 Sep 2023 18:29:14 +0200 Subject: [PATCH 2/4] Allocate and assign conditionally. --- src/nrnoc/multicore.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/nrnoc/multicore.cpp b/src/nrnoc/multicore.cpp index 2de1d26d2a..5c716fd285 100644 --- a/src/nrnoc/multicore.cpp +++ b/src/nrnoc/multicore.cpp @@ -393,8 +393,9 @@ void nrn_threads_free() { tml2 = tml->next; free((char*) ml->nodelist); free((char*) ml->nodeindices); - free((char*) ml->prop); - if (!memb_func[tml->index].hoc_mech) { + if (memb_func[tml->index].hoc_mech) { + delete [] ml->prop; + } else { free((char*) ml->pdata); } if (ml->_thread) { @@ -500,8 +501,10 @@ printf("thread_memblist_setup %lx v_node_count=%d ncell=%d end=%d\n", (long)nth, mlmap[i] = tml->ml; CACHELINE_ALLOC(tml->ml->nodelist, Node*, mlcnt[i]); CACHELINE_ALLOC(tml->ml->nodeindices, int, mlcnt[i]); - tml->ml->prop = new Prop*[mlcnt[i]]; // used for ode_map - if (!memb_func[i].hoc_mech) { + if (memb_func[i].hoc_mech) { + tml->ml->prop = new Prop*[mlcnt[i]]; // used for ode_map + } + else { CACHELINE_ALLOC(tml->ml->pdata, Datum*, mlcnt[i]); } tml->ml->_thread = (Datum*) 0; @@ -528,8 +531,10 @@ printf("thread_memblist_setup %lx v_node_count=%d ncell=%d end=%d\n", (long)nth, Memb_list* ml = mlmap[p->_type]; ml->nodelist[ml->nodecount] = nd; ml->nodeindices[ml->nodecount] = nd->v_node_index; - ml->prop[ml->nodecount] = p; - if (!memb_func[p->_type].hoc_mech) { + if (memb_func[p->_type].hoc_mech) { + ml->prop[ml->nodecount] = p; + } + else { // ml->_data[ml->nodecount] = p->param; ml->pdata[ml->nodecount] = p->dparam; } From 84d3e65e4834ad560843d737e8d049b42de7ed5c Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Thu, 28 Sep 2023 10:45:50 +0200 Subject: [PATCH 3/4] Revert "Allocate and assign conditionally." This reverts commit 692a72457389d1fd5a2cebd77554fcde5bec04ab. --- src/nrnoc/multicore.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/nrnoc/multicore.cpp b/src/nrnoc/multicore.cpp index 5c716fd285..2de1d26d2a 100644 --- a/src/nrnoc/multicore.cpp +++ b/src/nrnoc/multicore.cpp @@ -393,9 +393,8 @@ void nrn_threads_free() { tml2 = tml->next; free((char*) ml->nodelist); free((char*) ml->nodeindices); - if (memb_func[tml->index].hoc_mech) { - delete [] ml->prop; - } else { + free((char*) ml->prop); + if (!memb_func[tml->index].hoc_mech) { free((char*) ml->pdata); } if (ml->_thread) { @@ -501,10 +500,8 @@ printf("thread_memblist_setup %lx v_node_count=%d ncell=%d end=%d\n", (long)nth, mlmap[i] = tml->ml; CACHELINE_ALLOC(tml->ml->nodelist, Node*, mlcnt[i]); CACHELINE_ALLOC(tml->ml->nodeindices, int, mlcnt[i]); - if (memb_func[i].hoc_mech) { - tml->ml->prop = new Prop*[mlcnt[i]]; // used for ode_map - } - else { + tml->ml->prop = new Prop*[mlcnt[i]]; // used for ode_map + if (!memb_func[i].hoc_mech) { CACHELINE_ALLOC(tml->ml->pdata, Datum*, mlcnt[i]); } tml->ml->_thread = (Datum*) 0; @@ -531,10 +528,8 @@ printf("thread_memblist_setup %lx v_node_count=%d ncell=%d end=%d\n", (long)nth, Memb_list* ml = mlmap[p->_type]; ml->nodelist[ml->nodecount] = nd; ml->nodeindices[ml->nodecount] = nd->v_node_index; - if (memb_func[p->_type].hoc_mech) { - ml->prop[ml->nodecount] = p; - } - else { + ml->prop[ml->nodecount] = p; + if (!memb_func[p->_type].hoc_mech) { // ml->_data[ml->nodecount] = p->param; ml->pdata[ml->nodecount] = p->dparam; } From 9cc76c1b74d465c6c6c59e4a596a7249be646ad0 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Thu, 28 Sep 2023 10:48:39 +0200 Subject: [PATCH 4/4] Mix misaligned new/free pair. --- src/nrnoc/multicore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nrnoc/multicore.cpp b/src/nrnoc/multicore.cpp index 2de1d26d2a..ac2dbdd70e 100644 --- a/src/nrnoc/multicore.cpp +++ b/src/nrnoc/multicore.cpp @@ -393,7 +393,7 @@ void nrn_threads_free() { tml2 = tml->next; free((char*) ml->nodelist); free((char*) ml->nodeindices); - free((char*) ml->prop); + delete[] ml->prop; if (!memb_func[tml->index].hoc_mech) { free((char*) ml->pdata); }