Probable pointer error somewhere

Hi!

My program runs for some time, then comes to a line it has executed several times earlier and crashes.

the problem:
1
2
3
4
5
void Block::setTerrain(string type, double value){
    cout << "test" << endl;
    m_Terrain[type] = value; //crash
    return;
}




and it's called from a function that runs until m_ActiveSeeds is empty and the function can't be called if m_ActiveSeeds.empty():
1
2
    Block *p = m_ActiveSeeds.at(0); //been changing around between begin(), front() and at(0) to see if that was the problem
    p->setTerrain(type, 1);


m_Terrain:
1
2
//in the Block class:
map<string, double> m_Terrain;


m_ActiveSeeds:
1
2
//in Map class
std::vector<Block*> m_ActiveSeeds;


I can't figure out what's wrong, I thought it was some other error but when I tracked it down it was just that line that should work whatever the situation as long as type is a string value is an int/double and m_Terrain is initiated.

Any help?
Last edited on
when it crashes what does your map contain and what's the current value of "type"?
and what error message(s) do you get when it does blow up?
Last edited on
This Block *p = m_ActiveSeeds.at(0); will crash when ActiveSeeds is empty, hence
1
2
3
4
5
6
7
if(m_ActiveSeeds.empty())
    ;
else
{
    Block *p = m_ActiveSeeds.at(0);
    p->setTerrain(type, 1);
}


Another problem could be that isn't valid
Last edited on
m_ActiveSeeds is not empty when it crashes.

m_ActiveSeeds cannot be used when empty: all procautions used.

error message: -1073741819 <0xC0000005>

current type = "water"

current value = 1

added a line first in the code to be sure:
if(m_ActiveSeeds.empty()) cout << "fuck they were right" << endl;

and it is never printed.


It has to be that pointer, somehow...

Or wait... could be m_ActiveSeeds, it's not initialized in the constructor gimme a sec.

but that shouldn't make any difference since when the vector constructor is called it generates an empty vector...


hmm....
1
2
3
    Block *p = m_ActiveSeeds.front();
    cout << p << endl;
    cout << &m_ActiveSeeds.front() << endl;


returns 2 different addresses should they?
Last edited on
returns 2 different addresses should they?
Yes, &m_ActiveSeeds.front() is the address of the pointer


This line
m_Terrain[type] = value;
can only crash when the encompassing object (Block) is invalid.

do you delete a Block form the vector anywhere?
Maybe such an invalid object remains within m_ActiveSeeds
I add pointers at the end and remove the first pointer in the vector when I'm done with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool Map::addSeed(Block* test){
    for(std::vector<Block*>::iterator it = m_ActiveSeeds.begin(); it != m_ActiveSeeds.end(); it++){
        if(checkSameID(test, *it)){
            return false;
        }
    }
    m_ActiveSeeds.push_back(test);
    return true;
}




        for(std::vector<Block*>::iterator it = temp.begin();it != temp.end();it++){
            if(addSeed(*it)) cout << "success" << endl;;
        }


m_ActiveSeeds.erase(m_ActiveSeeds.begin());



still no solution? been staring at this code for a very long time now.
Last edited on
Entire GenMap.cpp: all extra couts are only to track down the problem
Last edited on
This is too much code for me to comb through visually to find a problem. This is probably just going to take some good old-fashioned debugging. Be happy that the problem is easy to reproduce ;)

Adding 'cout' lines is a poor man's debugger. You should really learn how to use a real debugger, as it tells you so much more. (set breakpoints, step through code, examine variable contents, etc, etc).

I go through the basics of using a debugger here:

http://www.cplusplus.com/forum/beginner/75304/#msg403990


I'd be willing to help debug the code if you can zip up and upload the full source (and any necessary external files... like "1.txt") for me somewhere.
Will get to it soon, just need to take an hour or two off from code, seeing * everywhere.
Ok, been messing about with the debugger for a while, and it didn't make more sense (possibly because this is the first time i try to debug)

but it runs that same function 152 times without error.
m_ActiveSeeds contains Block* and varies in size as it should...

hmm.. wait. The Block* it calls that 153th time is not like the others...//it crashes after this

Ok... let's see when it get's there...

hmm... I can't seem to find when it get's here...

ah well, let's take it one add at a time then since they all get added at the end.

ok, the 117th time it adds a memory address that is empty...
and at 149 the pointer that crashes the program is added.

they both have a 0x29 address when everything else inside the m_ActiveSeeds have a 0x28 address.

both is added when this part is called:


ok think I located the problem, the edges have strange IDs,.

so this function:
1
2
3
4
5
6
7
8
void Map::setIDS(int x, int y){
    for(int i = 0; i < x; i++){
        for(int j = 0; j < y; j++){
            m_Map[i][j].setID(i,j);
        }
    }
    return;
}


actually more then the edges...
Last edited on
I'm very much lost here...setIDS doesn't set all the IDs correctly, how is that?
Assuming the ID is the same as its coordinate, it looks like it would be working just fine to me.

It's hard to say what's going on because I've only seen fragments of the code. I don't think I can help any further with this one unless I have full compilable source.
Haven't found any good upload locations. Any tips?
there's all sorts of free online storage sites. Dropbox is a popular one. But there's other stuff like megaupload (Though that's usually used for porn)
https://www.dropbox.com/s/jzp0yuqw0c835r1/MapGen.7z?v=0mcn

All the files I use.

u might have to rewrite the save location for the Map::saveCurrentMap function
Last edited on
I'll check this out when I get some free time. I can't guarantee I'll get to it tonight.
Ok, sweet. Because I'm on the brink of rewriting everything soon...

Ok, I'm confused, it ran for 565 times just now... I'm guessing because it had different stuff in the memory this time.
Last edited on
Eureka! quite literally... was in the bath...
this function:
1
2
3
4
5
6
int mapxModulos(int nr, int div){
    if(nr >= 0 ){return nr % div;}
    else{
        return (div - nr);// Lookie Lookie since nr is negative, the return is larger then div....
    }
}


The problem should be fixed now, it just crashes when it runs out of memory now, so some logic error along the way, but I can fix that. Thanks all!
Topic archived. No new replies allowed.