From d4b779459a139c2d01f06259a976e18b1819122d Mon Sep 17 00:00:00 2001 From: Hinko Kocevar Date: Thu, 28 Mar 2019 11:44:17 +0100 Subject: [PATCH 1/4] fix memory leak by calling H5close() in NDFileHDF5::closeFile() --- ADApp/pluginSrc/NDFileHDF5.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ADApp/pluginSrc/NDFileHDF5.cpp b/ADApp/pluginSrc/NDFileHDF5.cpp index 380a2dd2c..0a9151b75 100644 --- a/ADApp/pluginSrc/NDFileHDF5.cpp +++ b/ADApp/pluginSrc/NDFileHDF5.cpp @@ -1658,10 +1658,20 @@ asynStatus NDFileHDF5::closeFile() "%s::%s Closing file not totally clean. Attributes remaining=%d\n", driverName, functionName, obj_count); } + obj_count = (int)H5Fget_obj_count(this->file, H5F_OBJ_ALL); + if (obj_count > 0){ + asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, + "%s::%s Closing file not totally clean. All remaining=%d\n", + driverName, functionName, obj_count); + } // Close the HDF file H5Fclose(this->file); this->file = 0; + // Flush all data to disk, close all open HDF5 objects, + // and clean up all memory used by the HDF5 library + // to avoid memory leaks + H5close(); // At this point we can clear the SWMR active flag, whether we were running // in SWMR mode or not From 684f52826b79ac79c07c0efc9f5c89f933e6fbbc Mon Sep 17 00:00:00 2001 From: Hinko Kocevar Date: Thu, 28 Mar 2019 12:02:29 +0100 Subject: [PATCH 2/4] print error if remaining count is >1 (file object is closed later) --- ADApp/pluginSrc/NDFileHDF5.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ADApp/pluginSrc/NDFileHDF5.cpp b/ADApp/pluginSrc/NDFileHDF5.cpp index 0a9151b75..5bdc20f24 100644 --- a/ADApp/pluginSrc/NDFileHDF5.cpp +++ b/ADApp/pluginSrc/NDFileHDF5.cpp @@ -1659,9 +1659,9 @@ asynStatus NDFileHDF5::closeFile() driverName, functionName, obj_count); } obj_count = (int)H5Fget_obj_count(this->file, H5F_OBJ_ALL); - if (obj_count > 0){ + if (obj_count > 1){ asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, - "%s::%s Closing file not totally clean. All remaining=%d\n", + "%s::%s Closing file not totally clean. Other remaining=%d\n", driverName, functionName, obj_count); } From a8ae7fb51009c785857567c1775d50f1c3d86bac Mon Sep 17 00:00:00 2001 From: Hinko Kocevar Date: Thu, 28 Mar 2019 17:17:34 +0100 Subject: [PATCH 3/4] fix #392 memory leak --- ADApp/pluginSrc/NDFileHDF5.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ADApp/pluginSrc/NDFileHDF5.cpp b/ADApp/pluginSrc/NDFileHDF5.cpp index 5bdc20f24..4cb73e869 100644 --- a/ADApp/pluginSrc/NDFileHDF5.cpp +++ b/ADApp/pluginSrc/NDFileHDF5.cpp @@ -2699,7 +2699,7 @@ asynStatus NDFileHDF5::createAttributeDataset(NDArray *pArray) // In here we need to open the dataset for writing hdf5::DataSource dsource = dset->data_source(); - std::string atName = std::string(epicsStrDup(ndAttr->getName())); + std::string atName = std::string(ndAttr->getName()); NDFileHDF5AttributeDataset *attDset = new NDFileHDF5AttributeDataset(this->file, atName, ndAttr->getDataType()); attDset->setDsetName(dset->get_name()); attDset->setWhenToSave(dsource.get_when_to_save()); From 4cfb14baa22ddbe98dd0b12d21b32b442c7ca063 Mon Sep 17 00:00:00 2001 From: Hinko Kocevar Date: Thu, 28 Mar 2019 17:25:36 +0100 Subject: [PATCH 4/4] plug memory leak ==21315== 4,000 bytes in 1,000 blocks are definitely lost in loss record 6,503 of 6,903 ==21315== at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21315== by 0x510CE7E: NDFileHDF5::createAttributeDataset(NDArray*) (NDFileHDF5.cpp:2652) ==21315== by 0x510E7C9: NDFileHDF5::openFile(char const*, int, NDArray*) (NDFileHDF5.cpp:282) ==21315== by 0x50FDEF5: NDPluginFile::openFileBase(int, NDArray*) (NDPluginFile.cpp:73) .. --- ADApp/pluginSrc/NDFileHDF5.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ADApp/pluginSrc/NDFileHDF5.cpp b/ADApp/pluginSrc/NDFileHDF5.cpp index 4cb73e869..648e96eee 100644 --- a/ADApp/pluginSrc/NDFileHDF5.cpp +++ b/ADApp/pluginSrc/NDFileHDF5.cpp @@ -2765,6 +2765,9 @@ asynStatus NDFileHDF5::createAttributeDataset(NDArray *pArray) if(def_group != NULL){ H5Gclose(groupDefault); } + if (numCapture) { + free(numCapture); + } return asynSuccess; }