GithubHelp home page GithubHelp logo

Call the destructor twice about entt HOT 1 CLOSED

liziyi75 avatar liziyi75 commented on June 21, 2024
Call the destructor twice

from entt.

Comments (1)

skypjack avatar skypjack commented on June 21, 2024

EnTT uses a simplified swap-and-pop policy by default (unless you enable something like the in-place delete).
The internals are a little more complicated than what I'm going to describe here but imagine the following for a moment:

  • You swap the last element with the one to remove. Swapping involves a third (and temporary) element that is discarded immediately after. First destructor invoked.
  • You pop the last element in the set. Second destructor invoked.

Here we go with your destructors. Two elements, two invocations. That's all.
The fact that you've a single element doesn't really matter. EnTT doesn't place all this after an if that only works in a single case. It would be a waste of cpu cycles for nothing. Instead, the library uses a safe approach that works in all cases.


That being said and for the sake of curiosity, EnTT works in a slightly different manner internally but the final result is similar.
What happens is that the last element is moved into a call to std:.exchange with the element to drop which is returned rather than discarded immediately. Then we pop the last (moved-from) element and you've the first destructor invoked. Finally we let the element to discard go out of scope and you have the second destructor invoked.
Why it's that complicated? Well, all this is meant to support re-entrant destructors. A rather sophisticated feature that most users don't use (and not even know about actually) but which is also very powerful in case you need it.

from entt.

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.