RunTime Error

Hi guys,

Yesterday I've changed something in my program and didnt debug before I closed it. What's wrong about the CTile::GetTypeID()- function? I dont know anymore what exactly I changed, but I'm certain that I havn't changed that function...

#0 00405B10 CTile::GetTypeID(this=0x5244)
#1 00404397 CMap::GetTypeOfTile(this=0x4851f4, X=3, Y=5)
#2 00404963 CPlayer::Collision_Hor(this=0x485158, x=100, y=183, TileCoordY=@0x28fbdc: 5, Map=0x4851f4)
#3 004051FE CPlayer::Update(this=0x485158, Map=0x4851f4)
#4 00401E8E CGame::OnUpdate(this=0x485120)
#5 004021A1 SDL_main(argc=argc@entry=1, argv=argv@entry=0x330008)
#6 0040667C console_main(argc=argc@entry=1, argv=argv@entry=0x330008)
#7 0040683D WinMain@16(hInst=0x400000, hPrev=0x0, szCmdLine=0x7341d9 "",
#8 0047786B main () (??:??)


Again I think all of the Errors are coming from the GetTypeID()-function, which is being used in CMap::GetTypeOfTile(). CMap::GetTypeOfTile() is used in CPlayer::Collision_Hor() and so on...

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
#ifndef _CTILE_H_
    #define _CTILE_H_

#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include "CSprite.h"


enum {TILE_TYPE_NONE = 0,
      TILE_TYPE_SOLID = 1,
      TILE_TYPE_WALKABLE = 2,
      TILE_TYPE_DMG = 3,
      TILE_TYPE_CLIMBABLE = 4,
      TILE_TYPE_UNDERWATER = 5,
      TILE_TYPE_COLLECTABLE = 6};


class CTile
{
    public:
        CTile();

        int TempTileID;
        int TempTypeID;

        int GetTileID();
        int GetTypeID();
        int GetSize();
        void Animate();

        bool IsOnScreen(int X, int Y);
        int GetCurrentFrame() { return CurrentFrame; }

    private:
        int TileID;
        int TypeID;

        int CurrentFrame;

        static const int TILE_SIZE = 32;
};


#endif // _CTILE_H_ 


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
#include "CTile.h"
#include "CCamera.h"

CTile::CTile()
{
    TileID = 0;
    TypeID = TILE_TYPE_NONE;
    CurrentFrame = 0;
}

int CTile::GetSize()
{
    return TILE_SIZE;
}

int CTile::GetTileID()
{
    return TileID;
}

int CTile::GetTypeID()
{
    return TypeID;
}

bool CTile::IsOnScreen(int X, int Y)
{
    TileID = TempTileID;
    TypeID = TempTypeID;


    if (X + TILE_SIZE >= CCamera::Camera.Viewport.x &&
        X <= CCamera::Camera.Viewport.x + CCamera::Camera.Viewport.w &&
        Y + TILE_SIZE >= CCamera::Camera.Viewport.y &&
        Y <= CCamera::Camera.Viewport.y + CCamera::Camera.Viewport.h)
    {
         return true;
    }
    else
    {
        return false;
    }
}

void CTile::Animate()
{
    if (CurrentFrame <= 4)
    {
        CurrentFrame++;
    }
    else
    {
        CurrentFrame = 0;
    }
}
Why the TempTileID and TempTypeID?
You are defining TileID and TypeId as them in IsOnScreen() while I belive they are initialized.
Its more convenient for the maploading... I'm using fscanf to load the map and this way im able to just put the variable Name there (since they are public)
I understand your reason for doing that in the map loading, but you aren't gaining anything.

Follow my logic:
1) You are choosing not to expose TileID and TypeID for good reason (you don't want very many public variables). Okay. Great! I love it!
2) You instead expose the temps. Okay. So far I'm tracking with you...
3) Every frame, without fail, you set the TileID and TypeID to the same value as the temps. Woah! Hold the train. You might as well just expose the actual variables if you're going to set them to the same as the temps.

This adds an extra layer of nothing. You just have two extra variables to transfer the same values between public and private. The temps aren't actually used for anything useful as far as I can tell.

I think this is the heart of your problem. No where in your code do I see where the Temps are being assigned values. They could be getting them somewhere (and most likely are), but the fact that garbage values could be getting into your TypeID makes me suspicious.

Imagine this scenario.
- TempTileID gets value of 10 somehow.
- Now TileID gets that value.
- Program tries to read TileID, but wait... TileID is an enumerator that only goes up to 6.
- Computer goes djsbskfbdkamanslxbdjenska... Runtime error.

See what I mean?
I hope I don't sound too condescending, but it presents an issue when you are exposing variables like that. Might I suggest a nice collection of getters and setters instead? :)
Hmm I do see what you mean... I guess I'd Need a set-function?

but how would I use a set-function with fscanf? I only created those Temporary Variables to have easy Access and because my tries to implement two set-functions failed...

Info: the TileID (what Kind of tile, e.g. Brick or Water) itself isnt limited, only the TypeID (e.g. Solid / walkable). I dont think this is relevant tough, since I'm getting a runtime error even when I'm loading a map only consisting of 0:0 or 1:1.

with set-functions you mean void SetTypeID(int TempTypeID) { TypeID = TempTypeID; } void SetTileID (int TempTileID) { TileID = TempTypeID;}
Last edited on
That is exactly what I mean by a setter. However, if you don't do any error checking within the setter, then you are still in the same dilemma of possible garbage values. The point of the setter is to give the program the opportunity to consistently check for errors upon receiving a value and throw an error in a safe manner.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void SetTypeID(int _temp)
{
    if (_temp <= 6)
        TypeID = _temp;
    else
        cout << "Error" << endl;
}

...

// fscanf stuff into a temporary variable
int temp = 0;
fscanf(file, temp); // i forgot the syntax for fscanf
MyObj.SetTypeID(temp);


Let us know if this was actually the problem. You might even do some minor debugging to see what the values of your variables are upon failing to simply confirm.
Sorry I suddenly felt ill but after 16hours of sleep im Feeling way better ;)

I adjusted my code as we said, but its still not working properly :( I'm no longer getting a runtime error, but the only Thing appearing is a White Screen and I cant Close to window or anything (i think its called Feedback). After clicking the "X" a few times, no Feedback appears in the title of the window... The Sound is still playing, so i guess it's really about the map loading /Rendering.

Do you Need my code? Or do you any other idea what I'm doing wrong?
Last edited on
Topic archived. No new replies allowed.