Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

c++ - Unhandled exception at 0x000FBA44 in Top Down Shooter

I recently posted a question dealing with linker errors... Well for whatever reason those errors went away and is replaced with this. When I try to run my program, the window opens and it appears to run, however Visual Studio 2013 then presents me with the error:

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

And then takes me to a xutility file with a breakpoint here:

 #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;
                }

The arrow is on the _Mynextiter line. Does anyone know what is happening? I was initially using iterators to help go through some lists that I had, but I commented them out yet I still get this error and I'm not sure why

Edit: Ok, So after going back through the stack of methods called, the last piece of code that was called that was mine was this:

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);
            }

        }
    }
}

Now I remember when I wrote this I thought it was strange because loadedChunks is an std::list... I have never used lists so I thought it was strange that the list would only accept a pointer to a pointer to an object where in the <> of the list it clearly takes an object... I think this might be the source of my problem but I don't know how to fix it

Second Edit: Here is the ChunkManager class so you can see the lists I have

#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);
        }
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code denotes some confusion on very fundamental concepts like value and identity in C++. For example in

AbstractChunk *chunk = generateChunk(0, 0);

seems that generateChunk will allocate an object on the free store. Then however in:

loadedChunks.push_back(*chunk);

you are storing a copy of the allocated object in a container and the pointer is never used later (thus leaking the object).

Wildly guessing from the name, AbstractChunk is an abstract class with derived classes and the list should be an heterogeneous list of chunks of different types.

This is simply not possible in C++ (see the fundamental concepts of "slicing" and "copy semantic" of C++). You need to use a list of pointers to chunks instead.

Note that piling up a long stream of statements without understanding deeply how things works is a suicide strategy with C++. Even the fact that you assume that if you make a mistake the system will tell you so denotes you don't know how C++ works (see "undefined behavior" concept).

C++ cannot be learned by experimentation. You need to read a good book or two from cover to cover first.

There is no way to learn C++ except than by reading (and the smarter you are the worse the guessing approach will work... the reason is that in quite a few places the correct answer is not logical, but a consequence of an historic accident).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...