C++ - 删除包含 vector 的结构 vector 会导致内存泄漏吗?

标签 c++ memory-management memory-leaks vector struct

我正在构建一个游戏,并且有一个称为 Sprite 的对象 vector 。

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)
};

正如您在底部看到的那样,它本身包含一个子 Sprite 子 vector 。如果我从我的 sprite vector 中删除一个元素,它会导致 spritechild vector 发生内存泄漏还是会被处理?

谢谢!

最佳答案

你的 vector 被分配为 Sprite 结构的成员(它不是通过 new 分配的),所以当 Sprite 被删除时它会被自动清理。

当您通过 new 创建对象并且不delete 它时,您会遇到内存泄漏。

关于C++ - 删除包含 vector 的结构 vector 会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042780/

相关文章:

c++ - '_Unwind_GetIPInfo' 符号

c++ - 会在动态中重复分配一个 char 数组导致问题吗?

c++ - 多线程环境下的同名变量

c++ - 从 .dat 文件中仅提取几列

c - Valgrind 检测到可到达的泄漏,它们在哪里?

java - 如何查找由java代码引起的 native 内存泄漏?

c++ - 有人可以用 open mpi 解释这个 valgrind 输出吗?

c - 从 block 外访问 block 内自动变量的内存?

c - 嵌入式应用程序中的内存管理资源

IPv6网络接口(interface)程序检查中的Java堆外内存泄漏