Giter Site home page Giter Site logo

Comments (3)

terhorstd avatar terhorstd commented on June 1, 2024

The explicit clear() is necessary, because the vector contains explicit pointers to inner vectors. The delete removes all elements of the outer vector (pointers!), but the inner vector instances need to have their elements removed before the pointer is deleted.
Note that the clear() call does a double "dereferencing": it is the iterator, *it is a pointer and -> dereferences to the vector<>.

from nest-simulator.

heplesser avatar heplesser commented on June 1, 2024

@terhorstd I don't quite agree with your analysis. We have

std::vector< std::vector< SpikeDataWithRank >* > emitted_spikes_register_;

and

auto it = emitted_spikes_register_.begin();

so *it gives us the first element of the outer vector, i.e., a pointer to an std::vector<SpikeDataWithRank> object. The entries in this vector are non-pointer entries, and (*it)->clear() clears these entries. Afterwards,

delete (*it);

destroys the std::vector<SpikeDataWithRank> object that *it points to. In my understanding of C++ containers, deleting a container that contains only non-pointer objects, will properly delete all objects contained in the container, i.e., call their destructors, before deleting the container itself. Therefore, the (*it)->clear() is superfluous. Afterwards,

emitted_spikes_register_.clear(); 

removes the now invalid pointers.

from nest-simulator.

terhorstd avatar terhorstd commented on June 1, 2024

After our discussion we concluded that indeed this should also do:

 for ( auto vec_spikedata_ptr : emitted_spikes_register_ ) 
 { 
   delete vec_spikedata_ptr; 
 } 
 emitted_spikes_register_.clear(); // remove stale pointers

from nest-simulator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.