Segmentation Fault Problem

I have a pointer to a Level class and when I change level I want to delete that pointer (to free the memory) and construct a new Level with the same pointer curLevel_ = new Level(string)

However when I do this I get a segmentation fault. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//system_state.cpp
SystemState::SystemState()
{
    curLevel_ = new Level(std::string("resources/files/level1.txt"));
}
void SystemState::onUpdate()
{
    if (curLevel_ != 0)
        curLevel_->Update();
}
void SystemState::onRender( Canvas& canvas )
{
    if (curLevel_ != 0)
        curLevel_->DrawLevel(canvas);
}
bool SystemState::onKey(const IKeyEvent::KeyEvent& key_event)
{
    //for testing purposes
    if(key_event.key_state == IKeyEvent::KeyEvent::KB_DOWN && key_event.key == IKeyEvent::KeyEvent::KB_SPC_KEY)
    {
        LoadLevel(std::string("resources/files/level2.txt"));
    }
    if(key_event.key_state == IKeyEvent::KeyEvent::KB_DOWN && key_event.key == IKeyEvent::KeyEvent::KB_ESC_KEY)
    {
        application.exit();
        return false;
    }

    return true;
}
void SystemState::LoadLevel(std::string filePath)
{
    curLevel_ = new Level(filePath); //segmentation fault
}
//Level.cpp
Level::Level(std::string filePath)
{
    SnakeFileReader sfr(filePath);
    ImageFile(sfr.ReadLine()).load(map_layout);
    mapWidth_ = sfr.ReadInt();
    mapHeight_ = sfr.ReadInt();
    level_cell_size_ = sfr.ReadInt();

    map_ = new TileData*[mapWidth_];

    for (int i = 0; i < mapWidth_; i++)
    {
        map_[i] = new TileData[mapHeight_];
    }

    int numUnwalk = 0;

    bool foundPlayerDZ = false;
    bool foundEnemyDZ = false;
    Vector3 playerDZStart;
    Vector3 enemyDZStart;

    int iterations = sfr.ReadInt();
    TileInfo mapTiles[iterations];
    for (int i = 0; i < iterations; i++)
    {
        mapTiles[i].filePath = sfr.ReadLine();
        mapTiles[i].readColour = Colour(sfr.ReadColour());
        mapTiles[i].canWalk = sfr.ReadInt();
    }

    bool test = false;

    for (int y = 0; y < mapHeight_; y++)
    {
        for (int x = 0; x < mapWidth_; x++)
        {
            Colour cellColour = map_layout.getPixel(x * level_cell_size_, y * level_cell_size_);
            for (int i = 0; i < iterations; i++)
            {
                if (IsSameColour(cellColour, mapTiles[i].readColour))
                {
                    test = true;
                    ImageFile(mapTiles[0].filePath).load(map_[x][y].tile);
                    if (!mapTiles[i].canWalk)
                    {
                        Image wall;
                        ImageFile(mapTiles[i].filePath).load(wall);
                        wall.setTransparentColour(Colour(255,0,255));
                        map_[x][y].tile.blit(wall);
                        unwalkableLocs_.push_back(Vector3(x, y, 0));
                        numUnwalk++;
                    }
                }
            }

            if (IsSameColour(cellColour, humanDropZone_))
            {
                test = true;
                if (!foundPlayerDZ)
                {
                    playerDZStart = Vector3(x,y,0);
                    foundPlayerDZ = true;
                }
                ImageFile(mapTiles[0].filePath).load(map_[x][y].tile);
            }
            else if (IsSameColour(cellColour, computerDropZone_))
            {
                test = true;
                if (!foundEnemyDZ)
                {
                    enemyDZStart = Vector3(x,y,0);
                    foundEnemyDZ = true;
                }
                ImageFile(mapTiles[0].filePath).load(map_[x][y].tile);
            }
        }
    }
    human_ =  new Player(sfr.ReadVector(), Size(level_cell_size_,level_cell_size_), HUMAN, this, sfr.ReadLine());
    computer_ = new Player(sfr.ReadVector(), Size(level_cell_size_,level_cell_size_), COMPUTER, this, sfr.ReadLine(), human_);
    human_->SetOpponent(computer_);
    computer_->SetupDropZone(enemyDZStart);
    human_->SetupDropZone(playerDZStart);
    SetupCollectables();
}


Can anyone see why? If I didn't de-allocate memory on the heap from the previous level could that cause this problem? I have been putting off creating all the destructors for the classes created by the level. Maybe that's the problem?

Thanks in advance
If you create something with new, you must at some point destroy it with delete.
If you create something with new[], you must at some point destroy it with delete[].

I hit ctrl+f on this page and search for "delete" and there were no results in your code.
This is not the whole code, only a small part of it that I believed the error may be getting caused in. So you think that the problem could be caused by not de-allocating memory stored in the heap?
martianxx wrote:
I want to delete that pointer [...] However when I do this I get a segmentation fault.
You claim that the segmentation fault occurs when you delete the pointer, however there is no "delete" in the code you posted.
I'd go with L B on checking for the deletes.

Also, when you call LoadLevel(), you aren't checking to see if curLevel_ is already allocated. You should check it and delete it if it isn't NULL in that function.
@kooth for that to work he'd have to have nulled out the pointer in the first place, which some programmers don't do (see "dangling pointer")
You claim that the segmentation fault occurs when you delete the pointer, however there is no "delete" in the code you posted.


Sorry for the confusion. I meant that it occurs when the level is loaded. I had in my head it could be to do with memory de-allocation which based on your posts seems to be a reasonably assumption.

If the pointer is not null when I delete it what would the implications be? I assume none (with the right destructors for the classes used by in level in place) as long as I re-assign it straight away to avoid a dangling pointer.
Topic archived. No new replies allowed.