C++ 向量访问冲突、空索引、迭代器不兼容

C++ 向量访问冲突、空索引、迭代器不兼容,c++,memory-management,vector,shared-ptr,access-violation,C++,Memory Management,Vector,Shared Ptr,Access Violation,我正在写一个游戏,我正在努力进行内存管理。我得到了一些不同的运行时错误。下面是一些代码: 在级别类中: typedef std::shared_ptr<Projectile> projectile_ptr; std::vector<projectile_ptr> projectileList; 现在在另外两个地方我读了清单。在游戏类中,当我在渲染前更新游戏状态时,以及在渲染器中,当要绘制投射物时 我像这样迭代向量: void Level::addProjectile(p

我正在写一个游戏,我正在努力进行内存管理。我得到了一些不同的运行时错误。下面是一些代码:

在级别类中:

typedef std::shared_ptr<Projectile> projectile_ptr;
std::vector<projectile_ptr> projectileList;
现在在另外两个地方我读了清单。在游戏类中,当我在渲染前更新游戏状态时,以及在渲染器中,当要绘制投射物时

我像这样迭代向量:

void Level::addProjectile(projectile_ptr ptr)
{
    projectileList.push_back(ptr);
}

//somewhere else in the code
projectile_ptr projectile(new Projectile(PT_BULLET, true, &p));
game->level->addProjectile(projectile);
for (std::vector<projectile_ptr>::iterator it = game->level->projectileList.begin(); it != game->level->projectileList.end();) {
    Projectile & projectile = *(*it); 

    if (projectile.isDestroyed) {
        it = game->level->projectileList.erase(it);
        break;
    }
    projectile.calculatePosition(ticks);
    //some other calculations
    ++it;
}

for (std::vector<projectile_ptr>::iterator it = game->level->projectileList.begin(); it != game->level->projectileList.end(); ++it) {
    Projectile & projectile = *(*it);
    if (projectile.isReadyToRender) {
        //drawing code
    }
}
for(std::vector::iterator it=game->level->projectlelist.begin();it!=game->level->projectlelist.end();){
射弹和射弹=*(*it);
if(射弹被销毁){
它=游戏->关卡->投射列表。擦除(它);
打破
}
射弹。计算位置(刻度);
//其他一些计算
++它;
}
对于(std::vector::iterator it=game->level->projectlelist.begin();it!=game->level->projectlelist.end();+it){
射弹和射弹=*(*it);
if(投射物。isReadyToRender){
//绘图代码
}
}
上面的代码有什么问题,为什么会出现奇怪的运行时

当shared_ptr负责删除对象时,向量中怎么可能有空白


我在擦除和渲染循环中都得到了运行时。请提供帮助。

像这样的问题应该包括以下内容:。代码中的任何地方都可能有bug。例如
新射弹(PT_BULLET、true和p)
可疑,可能是一个bug
SDL_点p2=SDL_点{launchPointRight->rect.x,launchPointRight->rect.y}
PT_BULLET
是一个枚举。它不是MCVE。你有没有遵循MM给你的链接?这是一个连接到帮助中心的链接,你在这里注册时看到了这个链接。我完全同意MM和LRO的观点,你应该发布一个MCVE来获得完整的帮助。只是一个小提示,我的感觉是:
if(sparket.isDestroyed){…}**else**{…}
。我想还有一个地方不见了。只是一种感觉。没有MCVE我们帮不了你更多。这是一个SDL2项目,你希望我如何向你展示整个例子?像这样的问题应该包括一个例子。代码中的任何地方都可能有bug。例如
新射弹(PT_BULLET、true和p)
可疑,可能是一个bug
SDL_点p2=SDL_点{launchPointRight->rect.x,launchPointRight->rect.y}
PT_BULLET
是一个枚举。它不是MCVE。你有没有遵循MM给你的链接?这是一个连接到帮助中心的链接,你在这里注册时看到了这个链接。我完全同意MM和LRO的观点,你应该发布一个MCVE来获得完整的帮助。只是一个小提示,我的感觉是:
if(sparket.isDestroyed){…}**else**{…}
。我想还有一个地方不见了。只是一种感觉。没有MCVE,我们帮不了你更多。这是一个SDL2项目,你想让我如何向你展示整个例子?