简体   繁体   中英

Using smart pointers correctly without leaks

I'm working on an architecture where I need to have an Entity in different list like that :

  • Renderer -> List of Component (SpriteComponent)
  • Collisioner -> List of Component (PhysicComponent)

I think smart pointer are the best solution to manage all these references, so I started to learn it, but I've got some problems and I'm sor sur if I use them correctly.


Explications :

I've got an Abstract class : IEntity

class IEntity {
  public:
    IEntity( World& world );
  private:
    World* world;
  public:
    std::tr1::weak_ptr<IEntity> weak_this;
};

typedef std::tr1::shared_ptr<IEntity> Entity;

And I've got a method to create entities in an EntityManager :

Entity EntityManager::createEntity() {
    Entity entity( new IEntity( *this->world ) );
    entity->weak_this = entity;
    this->entityList.add( &entity );
    return entity;
}

In my EntityManager class I've got a vector of "Entity" (Of shared_ptr) :

std::vector<Entity> entityList;

1 - Did i need to use the type "Entity" everywhere in my program (in parameters, ... ) ?

2 - If i have this :

class IComponent {
  public:
    IComponent();    
};
typedef std::tr1::shared_ptr<IComponent*> Component;

And i've got an object like this :

class SpriteComponent : public Component {
  public:
    SpriteComponent();        
    int texture;
};

It's good to inherit from a shared_ptr ? It's look strange for me but that work.

3 - I've tried to create 10000 entities using this :

Entity entity = world.getEntityManager().createEntity();

A reference to the entity is push in my vector of "Entity", if I have really understand smart pointer, a clear on vector will remove all Entity (because there isn't other references). But when I look with the cXode leak profiler I can see the memory growing without Entity removed. So I've just tried to create entity withour insertion in my vector and I haven't got leak, why ? Where is the problem ? Oo.

4 - If i use smart_ptr in a game, did i have some performance issues ? (I'm using references) :

Thanks for your time!

  1. I wouldn't call it Entity , but maybe EntityPtr or some such. ( Entity sugests that its a concrete implementation of IEntity )
  2. Don't derive from Component , but from IComponent . Your Component smart pointers will then be able to hold objects of type SpriteComponent .
  3. A clear on the EntityManager.entityList would delete the objects if no other references existsed. But your code seems to be doing something odd when filling the vector.

     EntityPtr entity( new IEntity( *this->world ) ); ... this->entityList.add( &entity ); 

    This adds the address of the entity to the entity list, there should not be a & there. I'm not sure why this would cause a leak - but its definately wrong.

  4. Yes, there is a performance penalty in using smart pointers. But dont worry about it until you can measure it as a problem in your core loops - It may never be enough of a penalty to matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM