簡體   English   中英

向量不能正確擦除內容(復制分配運算符的量運行直到崩潰[BEX])?

[英]vector does not erase content correctly (infite amount run of copy asignment operator untill crash [BEX])?

好吧,我的問題是,在我想“卸載”已加載的DLL之后,副本assignmnent運算符被稱為無限次數,直到崩潰。

從中刪除矢量數據的代碼如下所示:

void UnloadPlugins()
{
    dbg(("[DBG]UnloadPlugins()"));
    for(std::vector<DLLInfo>::iterator it = plugins.begin(); it != plugins.end(); ++it) 
    {
        plugins.erase(it);
    }
    dbg(("[DBG]UnloadPlugins()::Done"));
}

但是,永遠不會打印“ [DBG] UnloadPlugins():: Done”。

這是我的副本分配運算符:

// 2. copy assignment operator
DLLInfo& operator=(const DLLInfo& that)
{
    dbg(("[DBG]Start-DLLInfo& operator=(const DLLInfo& that)"));
    Instance = that.Instance;//hinstance
    dbg(("[DBG]DLLInfo 1"));
    //Identifier.assign(that.Identifier);//string
    dbg(("[DBG]DLLInfo 2"));
    IsAMX = that.IsAMX;//integer
    dbg(("[DBG]DLLInfo 3"));
    dwSupportFlags = that.dwSupportFlags;//integer
    dbg(("[DBG]DLLInfo 4"));
    Load = that.Load;//integer
    dbg(("[DBG]DLLInfo 5"));
    Unload = that.Unload;//integer
    dbg(("[DBG]DLLInfo 6"));
    Supports = that.Supports;//integer
    dbg(("[DBG]DLLInfo 7"));
    ProcessTick = that.ProcessTick;//integer
    dbg(("[DBG]DLLInfo 8"));
    AmxLoad = that.AmxLoad;//integer
    dbg(("[DBG]DLLInfo 9"));
    AmxUnload = that.AmxUnload;//integer
    dbg(("[DBG]DLLInfo 10"));
    UseDestructor = that.UseDestructor;//bool
    dbg(("[DBG]DLLInfo 11"));
    KeyboardHit = that.KeyboardHit;//integer
    dbg(("[DBG]End-DLLInfo& operator=(const DLLInfo& that)"));
    return *this;
}

因此,日志如下所示:

[17:50:50] [DBG]UnloadPlugins()
[17:50:50] [DBG]~DLLInfo    
[17:50:50] [DBG]~DLLInfo::if(this->UseDestructor) passed    
[17:50:50] [DBG]~DLLInfo::if(this->UseDestructor)::if(this->Unload != NULL && this->IsAMX) passed    
[17:50:50] [DBG]~DLLInfo::end    
[17:50:50] [DBG]Start-DLLInfo& operator=(const DLLInfo& that)    
[17:50:50] [DBG]DLLInfo 1   
[17:50:50] [DBG]DLLInfo 2    
[17:50:50] [DBG]DLLInfo 3    
[17:50:50] [DBG]DLLInfo 4    
[17:50:50] [DBG]DLLInfo 5    
[17:50:50] [DBG]DLLInfo 6    
[17:50:50] [DBG]DLLInfo 7    
[17:50:50] [DBG]DLLInfo 8    
[17:50:50] [DBG]DLLInfo 9    
[17:50:50] [DBG]DLLInfo 10    
[17:50:50] [DBG]DLLInfo 11    
[17:50:50] [DBG]End-DLLInfo& operator=(const DLLInfo& that)    
[17:50:50] [DBG]Start-DLLInfo& operator=(const DLLInfo& that)    
...  
[17:50:50] [DBG]End-DLLInfo& operator=(const DLLInfo& that)
...repeat until crash

可能是什么問題?

erase返回一個迭代器( 在此處提供文檔),而不是在您的for中使用++it ,您應該嘗試使用it = plugins.erase(it);

但是看到您的代碼,如果只erase所有內容,最好調用clear此處的文檔)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM