由结构向量引起的C++内存泄漏

由结构向量引起的C++内存泄漏,c++,memory-management,vector,memory-leaks,C++,Memory Management,Vector,Memory Leaks,所示线路导致内存泄漏。悬而未决的需求推回&f;在sendreq方法中。我是新的C++,所以我似乎无法弄清楚为什么内存泄漏发生。泄漏的内存大小为16字节 class Station { struct Frame { enum { Token, Data, Ack } type; // type of frame unsigned int src; // sourc

所示线路导致内存泄漏。悬而未决的需求推回&f;在sendreq方法中。我是新的C++,所以我似乎无法弄清楚为什么内存泄漏发生。泄漏的内存大小为16字节

class Station {
    struct Frame {
        enum { Token, Data, Ack } type;                  // type of frame
        unsigned int src;                                // source id
        unsigned int dst;                                // destination id
        unsigned int prio;                               // priority
    } frame;

    unsigned int stnId;
    static unsigned int requests;                        // total send requests (if needed)
    void data( Frame frame );                            // pass frame
    void main();                                         // coroutine main
    Station *nextStation;
    vector<Frame*> pendingSendReqs;
  public:
    Station( unsigned int id ) : stnId(id) { }
    ~Station() {
        for (int i = 0; i < pendingSendReqs.size(); i++) {
            delete pendingSendReqs.at(i);
            cout << "~: " << pendingSendReqs.at(i) << endl;
        }
    }
    //unsigned int getId() { return stnId; }
    void setup( Station *nexthop ) {                     // supply next hop
        //*nexthop is the object
        nextStation = nexthop;
        //cout << "size: " << sizeof(*nexthop) << endl;
    }
    void sendreq( unsigned int round, unsigned int dst, unsigned int prio ) { // store send request
        Frame f;
        f.type = Frame::Data;
        f.src = stnId;
        f.dst = dst;
        f.prio = prio;


        pendingSendReqs.push_back(&f); //MEMORY LEAK CAUSED BY THIS LINE
    }
    void start();                                        // inject token and start
};

您正在向量中存储指向局部变量的指针,这些变量将自动被销毁。这是违法的

 vector<Frame*> pendingSendReqs;
 // this is a vector of pointers to struct and not a vector of structs

void sendreq( unsigned int round, unsigned int dst, unsigned int prio ) {
    Frame f;     // this automatic variable will get destroyed when sendreq returns
    f.type = Frame::Data;
    f.src = stnId;
    f.dst = dst;
    f.prio = prio;


    pendingSendReqs.push_back(&f); //MEMORY LEAK CAUSED BY THIS LINE
    // because you're going to hold on to this address which will mean
    // nothing when this function returns
}

这不是内存泄漏

pendingSendReqs.push_back(&f); 
这是未来未定义的行为。您正在存储局部变量的地址。任何试图在函数范围外取消引用其中一个指针的行为都是未定义的行为

你必须问问自己是否真的需要一个指针向量。如果你不知道答案,很可能你不知道。

什么时候

void sendreq无符号整数舍入、无符号整数dst、无符号整数优先

完,


您的vector PendingSendReq将包含指向已消除的变量的指针,因为这些变量是局部变量,并且将包含垃圾,并将导致崩溃。

代码中没有结构向量。只需使用向量即可。不需要指针。目前,这是一个问题:是否删除了delete pendingSendReqs.ati;在切换到存储对象而不是指针之后?向量的析构函数将自动为您销毁它们,因此您不必手动调用delete来删除未使用new分配的内容。事实上,因为这是你在~Station内做的唯一一件事,你可以完全摆脱析构函数。那么在这种情况下,存储这个结构的正确方法是什么呢?
pendingSendReqs.push_back(f); // store the object's copy instead of it's address so that it outlives the life of this local
pendingSendReqs.push_back(&f);