Re: [netcdfgroup] c++ NcFile assignment doesn't work

Perhaps you are using an outdated version of the new c++ interface?
In the current version, the copy constructor and copy assignment
operator are disabled, precisely to avoid the kind of bugs you are
running into.  In the latest version, the following code will not
compile:

foo = NcFile("blah.nc", NcFile::replace);

(see this commit of 9 december 2013,
https://github.com/Unidata/netcdf-cxx4/commit/cf35fe8980479d8a414fdd8828d74bfc89cda036
)

When you write

    foo = NcFile("blah.nc", NcFile::replace);

A temporary NcFile object is created, then copy-assigned to foo, and
then destructed.  The destructor of the temporary NcFile closes the
NetCDF file, and foo now holds an invalid NetCDF id (the id of the
file that was just closed), which causes errors when you try to use it
later on.

This is likely also why you ran into trouble using NcFiles as class
members.

With c++11, it would be nice to have move assignment and construction
for NcFile.  Until then, your suggestion of using unique_ptr<NcFile>
is a good workaround.

cheers,

Thomas

From: Jack Stalnaker <jack.stalnaker@xxxxxxxxx>
Subject: Re: [netcdfgroup] c++ NcFile assignment doesn't work
Date: Fri, 24 Apr 2015 14:22:37 -0500

_______________________________________________
netcdfgroup mailing list
netcdfgroup@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/



  • 2015 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: