自上而下射击游戏中的0x000FBA44未处理的异常

时间:2015-07-02 05:16:32

标签: c++ iterator

我最近发布了一个处理链接器错误的问题......无论出于何种原因,这些错误都会消失并被替换为此。当我尝试运行我的程序时,窗口打开并且似乎运行,但Visual Studio 2013然后向我显示错误:

Unhandled exception at 0x000FBA44 in Top Down Shooter.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.

然后带我到带有断点的xutility文件:

 #if _ITERATOR_DEBUG_LEVEL == 2
            if (_Myproxy != _Parent_proxy)
                {   // change parentage
                _Lockit _Lock(_LOCK_DEBUG);
                _Orphan_me();
                _Mynextiter = _Parent_proxy->_Myfirstiter;
                _Parent_proxy->_Myfirstiter = this;
                _Myproxy = _Parent_proxy;
                }

箭头位于_Mynextiter线上。有谁知道发生了什么?我最初使用迭代器来帮助查看我的一些列表,但我对它们进行了评论但我仍然遇到此错误,我不确定原因

编辑: 好的,所以在回过头调用的方法之后,调用的最后一段代码是我的:

ChunkManager::ChunkManager(b2World *w){
    AbstractChunk *chunk = generateChunk(0, 0);

    loadedChunks.push_back(*chunk);

    for (int i = 0; i < 64; i++){
        for (int p = 0; p < 64; p++){

            if (std::rand() > .7){
                AbstractBlock block(i, p, 0, w);
            }

        }
    }
}

现在我记得当我写这篇文章时我认为它很奇怪因为loadedChunks是一个std :: list ...我从来没有使用过列表所以我觉得奇怪的是列表只接受一个指向指针的指针对象在&lt;&gt;中的位置它清楚地显示了一个对象...我认为这可能是我的问题的根源,但我不知道如何解决它

第二次编辑:这是ChunkManager类,所以你可以看到我有的列表

#pragma once

#include <iostream>
#include<list>
#include<vector>
#include "AbstractChunk.h"

#ifndef CHUNKMANAGER_H
#define CHUNKMANAGER_H

class ChunkManager
{
public:
    ChunkManager();
    ChunkManager(b2World *world);
    ~ChunkManager();
    bool isChunkLoaded(int x, int y);
    bool isChunkGenerated(int x, int y);
    void loadChunksArround(int x, int y);
    AbstractChunk* loadChunk(int x, int y);
    int unloadChunk(int x, int y);
    std::list<AbstractBlock>* getLoadedBlocks();
private:
    b2World *world;
    std::list<AbstractChunk> loadedChunks;
    std::list<AbstractBlock> loadedBlocks;
    AbstractChunk* generateChunk(int x, int y);
};

#endif

AbstractChunk.cpp:

    #include "AbstractChunk.h"


    AbstractChunk::AbstractChunk()
    {
    }

    AbstractChunk::AbstractChunk(int x, int y){
        xpos = x;
        ypos = y;
    }

    int AbstractChunk::getXpos(){
        return xpos;
    }

    AbstractChunk::~AbstractChunk()
    {
    }

AbstractBlock.cpp:

#include "AbstractBlock.h"


AbstractBlock::AbstractBlock()
{
}

AbstractBlock::AbstractBlock(int x, int y, float roation, b2World *world){

}

sf::Sprite AbstractBlock::draw(){
    sf::Sprite sprite;
    return sprite;
}

void AbstractBlock::destroy(b2World *world){

}


AbstractBlock::~AbstractBlock()
{
}

ChunkManager.cpp:

#include "ChunkManager.h"


ChunkManager::ChunkManager(){

}

//Ignore this, working on it now
void ChunkManager::destroy(){
    for (int i = 0; i < loadedChunks.size; i++){
        loadedChunks.
    }
}

ChunkManager::ChunkManager(b2World *w){
    AbstractChunk* chunk = generateChunk(0, 0);

    loadedChunks.push_back(chunk);

    for (int i = 0; i < 64; i++){
        for (int p = 0; p < 64; p++){

            if (std::rand() > .7){
                AbstractBlock block(i, p, 0, w);
            }

        }
    }
}


std::list<AbstractBlock>* ChunkManager::getLoadedBlocks(){
    return &loadedBlocks;
}


ChunkManager::~ChunkManager()
{
}

AbstractChunk* ChunkManager::generateChunk(int x, int y){

    if (!isChunkGenerated(x,y)){
        AbstractChunk chunk(x, y);
        return &chunk;
    }
    else
        return nullptr;
}

bool ChunkManager::isChunkGenerated(int x, int y){
    return false;
}

AbstractChunk* ChunkManager::loadChunk(int x, int y){
    return nullptr;
}

void ChunkManager::loadChunksArround(int x, int y){

    int chunkX = std::floor(x / 16);
    int chunkY = std::floor(y / 16);

    for (int i = -1; i < 2; i++){
        for (int p = -1; p < 2; p++){
            loadChunk(i, p);
        }
    }

}

1 个答案:

答案 0 :(得分:4)

您的代码表示对C ++中的基本概念(如值和身份)存在一些混淆。例如在

AbstractChunk *chunk = generateChunk(0, 0);

似乎generateChunk将在免费商店上分配一个对象。 然而在:

loadedChunks.push_back(*chunk);

您将已分配对象的副本存储在容器中,并且以后从不使用该指针(从而泄漏对象)。

从名称中猜测,AbstractChunk是一个带派生类的抽象类,列表应该是不同类型块的异构列表。

这在C ++中是不可能的(参见C ++的“切片”和“复制语义”的基本概念)。您需要使用指向块的指针列表。

请注意,如果不深入了解事情是如何运作的,那么堆积大量语句就是使用C ++的自杀策略。即使您假设如果您犯了错误,系统会告诉您,这表示您不知道C ++是如何工作的(请参阅“未定义行为”概念)。

C ++无法通过实验学习。您需要先从封面阅读a good book or two以覆盖。

除了通过阅读之外没有办法学习C ++(更聪明的是你猜测方法会更糟糕......原因是在很多地方,正确答案不符合逻辑,但结果是一场历史性事​​故)。

相关问题