Error in closing win32 application

Hello!

I've continued to work on a text based RPG I've been making progress on just for practice. I've come up with this problem though that I have searched for everywhere, and can't seem to find an answer to:

Everything I have so far runs entirely smoothly. Once my program ends the main function, though, it gives an error that says 'Program has encountered an error and needs to close.'
Because there are no compiler errors or warnings, I'm not sure where to start looking to fix this issue. I've messed around a lot with some of the source code which I thought might be the culprit, but so far nothing has worked.

Here is my main function for reference:

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
int main() {
  RgnList_t regions (MAX_REGIONS);
  Player_t player;

  // [[ PROGRAM EXECUTION ]]
  load_game( &regions[0] ); 
  vector<string> currentactions = do_action( "move_1000", &regions[0], &player );

  size_t delimiter;
  while ( currentactions[0] != "exit" ) { 

    for ( unsigned short i=0; i<currentactions.size(); i++ ) {
      delimiter = currentactions[i].find( "_" );
      if ( currentactions[i].substr(0, int(delimiter)) != "move" ) { 
        do_action( currentactions[i], &regions[0], &player ); 
      } 
      else { 
        currentactions = do_action( currentactions[i], &regions[0], &player ); 
        break; 
      } 
    } //FOR 

  } //WHILE 

  // Displays before error
  cout << "Test!" << endl;
  return 0;
}


do_action() returns a vector<string>, each string list being a set of actions which must be performed by do_action upon the next while iteration. 'currentactions' is reset when the final action has been performed. When vector<string> do_action()[0] is something other then "exit", the game runs very smoothly. Once I call "exit", though, it causes the main 'while' game loop to end. This causes the program to end, as it should, only with the error that I described above.

I'm not yet very experienced with C++, so I apologize if the information I've given is less then required or off the mark. If anyone has any ideas, I'll be happy to explain anything necessary in further detail.

Thanks in advance!
- Aaron
the error comes at the very last, which means error is coming from some destructor. I think you have messed with some of the objects you have created which are causing the problem during destruction.
how big is your program? if its not very big you can post the whole thing.
It's fairly lengthy I suppose, it has 8 classes. Only five of those classes are ever declared so far, the rest I have not programmed the functionality for yet.

I could try constructing and destructing some of those classes before the program ends, because of that is the case it would trigger the error and with that it could be narrowed down a little further.
Alright, it must be the destructor of the Player_t class. I tested this using this code:

1
2
3
	Player_t* player = new Player_t;
	system( "PAUSE" );
	delete player;


The system paused, and as soon as I resumed the program gave a debug error.
I commented out the destructor of player and ran a close, and it closed without error.

Here is the destructor (moved to a new function) for the player object:

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
void Player_t::saveplayerstats() {
  ofstream PlayerDataOutput( "Data/player_data.txt", std::ios::out );

  // Save Player attributes to file
  PlayerDataOutput << vigor << acumen << agility     << resilience << maxhp << maxmp
	               << dmg  << def         << deftype  << atkspd     << hp       << mp
	               << level << gold       << maxexp  << exp          << endl
	               << name;

  // Unequip items before saving inventory
  this->unequip( "WEAPON" );
  this->unequip( "ARMOR" );

  // Save Inventory
  Item_t* const BASEITEM (this->inventory.getitems());
  for ( unsigned short i=0; i<this->inventory.invsize(); i++ ) { // Loop through items
    short nextitemamount = inventory.getamount( BASEITEM + i );
    string nextitemname;
    for ( unsigned short j=0; j<(BASEITEM + i)->getname().length(); i++ ) { // Loop through current item name characters
      if ( (BASEITEM + i)->getname().substr(j, 1) != " " ) {
        nextitemname.append( (BASEITEM + i)->getname().c_str() );
      }
    }
    transform( nextitemname.begin(), nextitemname.end(), nextitemname.begin(), tolower );
    PlayerDataOutput << nextitemname << " " << nextitemamount << endl;
  }
  PlayerDataOutput.flush();
  PlayerDataOutput.close();
}


It saves the player's statistics and inventory to a file before the game ends so that statistics are kept next time the program is loaded. I've looked through it and I know I'm missing some sort of error but I can't seem to find what. I tried moving it into a new member function, and calling the new member function before the program closed, but that still gave an error.
Alright well this thread I believe is resolved.

1
2
3
4
5
    for ( unsigned short j=0; j<(BASEITEM + i)->getname().length(); i++ ) { // Loop through current item name characters
      if ( (BASEITEM + i)->getname().substr(j, 1) != " " ) {
        nextitemname.append( (BASEITEM + i)->getname().c_str() );
      }
    }


In this loop, I am using j as an iterator, but I accidentally iterated i instead of j, which obviously threw everything way off. Now that I changed it to j++, everything works perfectly.

Thanks very much for that suggested it definitely helped narrow it down. :)
great.
in most of the cases its the destructor who is the culprit when the application crashes after main is executed.
Topic archived. No new replies allowed.