gpt4 book ai didi

c++ - vector 没有正确删除内容(无限量地运行复制赋值运算符直到崩溃 [BEX])?

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:35 25 4
gpt4 key购买 nike

我的问题是,在我想“卸载”已加载的 DLL 之后,复制赋值运算符被无限次调用,直到崩溃。

我从中删除 vector 数据的代码如下所示:

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 返回一个迭代器(文档 here ),而不是 ++it 在你的 for 中,你应该尝试 it = plugins.erase(it );

但是看到你的代码,如果你只是erase一切,你最好调用clear(文档here)

关于c++ - vector 没有正确删除内容(无限量地运行复制赋值运算符直到崩溃 [BEX])?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13894238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com