This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>To: address@hidden >From: address@hidden >Subject: Memory leak question >Organization: . >Keywords: 199711101835.LAA26543 Hi John, > I've written some code in C++ that I can't seem to free up the memory > after I've used nc_close(). All my code does is open a netCDF file and > use a nc_get_var_short() to read in a 3D variable. I then close the > file with nc_close and wait for another file to be opened. The memory > allocated for the nc_get_var_short() is never returned. I'm working on > an SGI Octane running IRIX 6.3. Am I just not closing the netCDF file > properly or do I need to change over to the c version of netCDF. I can't duplicate the problem. If you invoke the nc_close() member function on the netCDF file before exiting, you are closing the file properly. I've run the appended test program using memory checking on a Solaris 2.6 system with netCDF 3.3.1, and it reports no memory leaks when the program terminates. We don't have access to an IRIX 6.3 system, but I've compiled and run the program on an IRIX 6.2 system. I tried to use the "purify" tool to check for memory leaks, but our current copy of purify gets a segmentation violation on this, so maybe it's not up to date. There is a patch for a memory leak problem in the C++ code available from our "Known Problems" page, at http://www.unidata.ucar.edu/packages/netcdf/known_problems.html#cxxleak but I think it may be unrelated to the problem you are reporting. Nevertheless, you might try that patch and see if it makes the symptoms go away. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu #include <stdio.h> #include <stdlib.h> #include "netcdf.hh" NcFile *ncfile; int main() { short t0[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; NcFile ofile = NcFile("jnk.nc", NcFile::Replace); ncfile = &ofile; if (!ofile.is_valid()){ cerr << "invalid ofile\n"; return(1); } NcDim* tdim = ofile.add_dim("time", 2); NcDim* lat = ofile.add_dim("lat", 3); NcDim* lon = ofile.add_dim("lon", 4); NcVar* tvar = ofile.add_var("temp", ncShort, tdim, lat, lon); if(!tvar->put(t0, 2, 3, 4)) cerr << "put failed\n"; ofile.close(); NcFile nfile = NcFile("jnk.nc"); NcVar* nvar = nfile.get_var("temp"); short temp[2][3][4]; if(!nvar->get(&temp[0][0][0], 2, 3, 4)) cerr << "get failed\n"; nfile.close(); }