Jump to content
  • Advertisement
Sign in to follow this  
Niddles

Memory leak?[fixed][program at end]

This topic is 4556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I have this RPG Map test program, and I'm having a little trouble with it. It compiles fine, but in runtime, windows puts up an error about it, and it closes. "RPGMAPTEST.exe has encountered a problem and needs to close. We are sorry for the inconvenience." That is the error, here is the code.
//RPG Map Test
//V 0.1
//Adam Martin
#include <iostream>
#include <string>
using namespace std;
bool playing;    
struct room { 
    int n, e, s, w;
    string description;
};
struct map {
    room MyMap[3][3];
    room* curroom;
    int currxpos;
    int currypos;
    void initMap();
    string* getdescrip();
    void changeroom(char in);
};
void map::initMap() {
    MyMap[1][1].description = "A room. Exits: S and E.";
    MyMap[1][1].e = 1;
    MyMap[1][1].s = 1;
    MyMap[1][2].description = "A hallway. Exits: S, E, N";
    MyMap[1][2].n = 1;
    MyMap[1][2].e = 1;
    MyMap[1][2].s = 1;
    MyMap[1][3].description = "A room. Exits: N and E.";
    MyMap[1][3].n = 1;
    MyMap[1][3].e = 1;
    MyMap[2][1].description = "A yard. Exits W, E, and S.";
    MyMap[2][1].e = 1;
    MyMap[2][1].w = 1;
    MyMap[2][1].s = 1;
    MyMap[2][2].description = "A well. Exits N, E, S, W.";
    MyMap[2][2].n = 1;
    MyMap[2][2].e = 1;
    MyMap[2][2].s = 1;
    MyMap[2][2].w = 1;
    MyMap[2][3].description = "A shop. Exits N, E, W";
    MyMap[2][3].n = 1;
    MyMap[2][3].e = 1;
    MyMap[2][2].w = 1;
    MyMap[3][1].description = "A bar. Exits S, W";
    MyMap[3][1].s = 1;
    MyMap[3][1].w = 1;
    MyMap[3][2].description = "A yard. Exits N, W, S";
    MyMap[3][2].n = 1;
    MyMap[3][2].s = 1;
    MyMap[3][2].w = 1;
    MyMap[3][3].description = "A random tree. Exits N, W";
    MyMap[3][3].n = 1;
    MyMap[3][3].w = 1;
    currxpos = 1;
    currypos = 1;
    curroom = &MyMap[currxpos][currypos];
}
string* map::getdescrip() {
    return &curroom->description;
}
void map::changeroom(char in) {
    if(in == 'X' || in == 'x') {
        playing = false;
    }
    if(in == 'N' || in == 'n') {
        currypos += 1;
    }
    if(in == 'S' || in == 's') {
        currypos -= 1;
    }
    if(in == 'E' || in == 'e') {
        currxpos += 1;
    }
    if(in == 'W' || in == 'w') {
        currxpos -= 1;
    }
    curroom = &MyMap[currxpos][currypos];
}
void gameLoop();
map gamemap; 
    char input;     
int main() {
    playing = true;
    while(playing == true) {
    gameLoop();
    }
    return 0;
}
void gameLoop() {
    cout<<"RPG MAP TEST."<<endl;
    cout<<*gamemap.getdescrip()<<endl; //THIS LINE
    cin>>input;
    gamemap.changeroom(input);
}


Now however if I take the dereference operator off of the 'gamemap.getdescrip()' on the line labeled 'THIS LINE' towards the end. It works fine, but outputs the description's address in memory. So I try putting the dereference operator on, and it doesn't work in runtime. Help please? [Edited by - Niddles on December 29, 2005 1:08:32 AM]

Share this post


Link to post
Share on other sites
Advertisement
I've tried every possibility I can think of, and that was one of them, it still compiles, but gives that same run-time error, sadly.

Share this post


Link to post
Share on other sites
Have you tried using the "c_str()" function on the string (both with and without returing a pointer to the string)? I mean, when you 'cout' the function, have you tried:

cout << gamemap.getdescrip().c_str(); << endl;

Share this post


Link to post
Share on other sites
Using the c_str() function still causes a runtime error, with a normal string function, and with a string pointer function, it says c_str() is undeclared. What exactly does c_str() do? I've never dealt with it.

Share this post


Link to post
Share on other sites
This is not a memory leak problem, but an access violation.

A memory leak happens when you dynamically allocate some memory, but do not keep a pointer to that memory so that it can later be released. An access violation happens when you try to access memory that hasn't been properly allocated.

Array indices are zero-based: if you have room MyMap[3][3];, the coordinates can vary from 0 to 2, not from 1 to 3.

Share this post


Link to post
Share on other sites
That does make sense. I changed it so it was right, but I'm still getting a run time error, if I try to output the dereferenced return value of the getdescrip function. If I just output it it gives me the address of it. This seems like it would be easily solved by dereferencing the return value of getdescrip, but it just creates an error.

Share this post


Link to post
Share on other sites
Well, for what it's worth, it seems you are never calling initMap(), which would mean your data structure contains garbage, and its pointers point to somewhere in Access Violation Land.

Share this post


Link to post
Share on other sites
Alright, here is my finished product, of my RPG Map Test. Tell me what you think of my programming design, and everything. Thanks for your help!

//RPG Map Test
//V 0.1
//Adam Martin
#include <iostream>
#include <string>
using namespace std;
bool playing;
struct room {
int n, e, s, w;
string description;
};
struct map {
room MyMap[3][3];
room* curroom;
int currxpos;
int currypos;
int num_errors;
void initMap();
string* getdescrip();
void changeroom(char in);
string error(string message);
};
string map::error(string message) {
num_errors++;
return message;
}
void map::initMap() {
MyMap[0][0].description = "A room. Exits: S and E.";
MyMap[0][0].e = 1;
MyMap[0][0].s = 1;
MyMap[0][1].description = "A hallway. Exits: S, E, N";
MyMap[0][1].n = 1;
MyMap[0][1].e = 1;
MyMap[0][1].s = 1;
MyMap[0][2].description = "A room. Exits: N and E.";
MyMap[0][2].n = 1;
MyMap[0][2].e = 1;
MyMap[1][0].description = "A yard. Exits W, E, and S.";
MyMap[1][0].e = 1;
MyMap[1][0].w = 1;
MyMap[1][1].s = 1;
MyMap[1][1].description = "A well. Exits N, E, S, W.";
MyMap[1][1].n = 1;
MyMap[1][1].e = 1;
MyMap[1][1].s = 1;
MyMap[1][1].w = 1;
MyMap[1][2].description = "A shop. Exits N, E, W";
MyMap[1][2].n = 1;
MyMap[1][2].e = 1;
MyMap[1][2].w = 1;
MyMap[2][0].description = "A bar. Exits S, W";
MyMap[2][0].s = 1;
MyMap[2][0].w = 1;
MyMap[2][1].description = "A yard. Exits N, W, S";
MyMap[2][1].n = 1;
MyMap[2][1].s = 1;
MyMap[2][1].w = 1;
MyMap[2][2].description = "A random tree. Exits N, W";
MyMap[2][2].n = 1;
MyMap[2][2].w = 1;
currxpos = 0;
currypos = 0;
curroom = &MyMap[currxpos][currypos];
}
string* map::getdescrip() {
return &curroom->description;
}
void map::changeroom(char in) {
if(in == 'X' || in == 'x') {
playing = false;
}
if(in == 'N' || in == 'n') {
if(curroom->n == 1) {
if(currypos - 1 < 0) {
cout<<error("You cant go there")<<endl;
}
else
currypos -= 1;
}
else
cout<<error("You cant go through a wall")<<endl;
}
if(in == 'S' || in == 's') {
if(curroom->s == 1) {
if(currypos + 1 > 2) {
cout<<error("You cant go there")<<endl;
}
else
currypos += 1;
}
else
cout<<error("You cant go through a wall")<<endl;
}
if(in == 'E' || in == 'e') {
if(curroom->e == 1) {
if(currxpos + 1 > 2) {
cout<<error("You cant go there")<<endl;
}
else
currxpos += 1;
}
else
cout<<error("You cant go through a wall")<<endl;
}
if(in == 'W' || in == 'w') {
if(curroom->w == 1) {
if(currxpos - 1 < 0) {
cout<<error("You cant go there")<<endl;
}
else
currxpos -= 1;
}
else
cout<<error("You cant go through a wall")<<endl;
}
curroom = &MyMap[currxpos][currypos];
}
void gameLoop();
map gamemap;
char input;
int main() {
playing = true;
cout<<"RPG MAP TEST."<<endl;
gamemap.initMap();
while(playing == true) {
gameLoop();
}
return gamemap.num_errors;
}
void gameLoop() {
cout<<*gamemap.getdescrip()<<endl;
cin>>input;
gamemap.changeroom(input);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!