C++ - Will deleting a vector of structs that contain vectors cause memory leaks?

I am building a game and have a vector of objects called sprites.

struct Sprite
    SpriteType texture;     // The texture enumeration
    float x;                // The x position of the sprite
    float y;                // The y position of the sprite
    float xVel;             // The x velocity of the sprite
    float yVel;             // The y velocity of the sprite
    int imgBlockX;          // The x block in the image
    int imgBlockY;          // The y block in the image
    int numFrames;          // The number of frames in the sprite animation
    int curFrame;           // The current frame of animation
    int delay;              // The delay between frame switches
    int elapsed;            // The amount of time on this frame
    long lastTime;          // The last update time
    long curTime;           // The current update time
    bool loop;              // Does this animation loop?
    int lifespan;           // The max lifespan of the sprite
    int order;              // 0 for first 1 for last
    bool hasChildren;       // Is this a parent sprite?
    int maxSize;
    std::vector<SpriteChild> children;// The sprites that are linked to this one (die when this one dies)

As you can see at the bottom, it contains a vector itself of sprite children. If I delete an element from my sprite vector will it cause a memory leak with the spritechild vector or is that taken care of?



Your vector is allocated as a member of the Sprite struct (it's not allocated via new), so it will be cleaned up automatically when the Sprite is deleted.

You get memory leaks when you create an object via new and do not delete it.

No matter how you allocate sprite vector, deleting an element from it will will deallocate vector children specific to that object. Believe SpriteChild is not a typedef for a pointer type but a struct name.

It will be taken care of if you don't mess up with SpriteChild. If it allocates something in constructor, make sure you free it in destructor. IIRC struct is not much different from class just it has as default public visibility of its members.

It will ONLY cause a leak if the destructor for SpriteChild is not properly defined (I.E. the destructor for SpriteChild needs to delete any dynamically allocated memory).

Deleting twice could cause a crash.

However, just deleting an object successfully allocated with new shouldn't result in a memory leak.

Need Your Help

asp.net 3.5 app - can not load assemblies, "Strong name signature could not be verified", only when deploying to client

c# iis-7 asp.net-3.5

Have developed an asp.net 3.5 application which consists of a we-site, some developed assemblies and some 3rd party assembles such as Telerik, Jayrock etc, all very much standard 3rd party apps.

Writing for loop outputs to CSV columns

python csv

I have a for loop that prints 4 details: