1
Unhandled exception at 0x53e83d80 in TestGame.exe: 0xC0000005: Access violation reading         
location 0xfeeefef6.

My C++ program throws an unhandled exception when I close my SFML window (causing the main class to return 0;

The code causing this is an std::ostringstream object, if I dont use it the problem doesnt occur..

void Orbs::UpdateText()
{
oss.str("");

oss << air_orbs.number;
air_orbs.text.setString(oss.str());
oss.str("");

oss.flush();

}

The class header:

#pragma once

#include <SFML/Graphics.hpp>
#include "FieldConst.h"
#include <sstream>

int const ORB_CHAR_SIZE = 20;
float const ORB_SCALE_SIZE = 0.25f;

struct  Orb
{
sf::Texture texture;
sf::Sprite sprite;
int number;
sf::Text text;

void Add(){
    number++;
}

void Remove(){
    number--;
}
};

class Orbs
{
public:
Orbs();
~Orbs();
void Render(sf::RenderWindow &target);
void UpdateText();

private:
Orb air_orbs, darkness_orbs, death_orbs, earth_orbs, electricity_orbs,
    fire_orbs, life_orbs, light_orbs, machine_orbs, metal_orbs, time_orbs,     water_orbs;

std::ostringstream oss;
};


Player::Player()
{
Player::name = "Player1";

Player::deck = new Deck();
Player::voidZone = new Deck();

Player::hand = new Hand();

Player::orbs = new Orbs();
}

. #pragma once

#include "IncludeCards.h"
#include "Orbs.h"

class Player
{
 public:
Player();
~Player();

std::string GetName();
Deck* GetDeck();
Hand* GetHand();
Deck* GetVoidZone();
Orbs* GetOrbs();


void DrawFromDeck();
void DiscardToVoid(Card *cardToDiscard);

//void UseCard ability/invoke/spell

private:
std::string name;
Deck *deck;
Hand *hand;

Deck *voidZone;

Orbs *orbs;
};

What can I do to fix it? Thanks in advance

  • A more concise test case would help a lot here, as would the code that instantiates the object which contains the `std::ostringstream` – 111111 Feb 25 '13 at 13:05
  • I think you should use a local variable for the stringstream in your function, as it seems you do not want to use the persistence of the member oss. – Nobody moving away from SE Feb 25 '13 at 13:05
  • @111111 I instanciate the object in the Player class, Orbs *orbs; then orbs = new Orbs(); – mickaelalliel Feb 25 '13 at 13:07
  • Making it a local variable inside the function does not fix the problem, it still crashes when closing the program – mickaelalliel Feb 25 '13 at 13:10
  • 1
    If C++11 is an option you should consider using [std::to_string](http://en.cppreference.com/w/cpp/string/basic_string/to_string) instead. – Nobody moving away from SE Feb 25 '13 at 13:11
  • @user2107339 show that code with the new Orbs please, my bet is that you are calling the member function of an invalid object. He means have the `std::stringstreaam` as variable in the function rather than as a member variable of the class. – 111111 Feb 25 '13 at 13:11
  • This is probably not the code causing the problem. Try and reproduce it in a (far) smaller example. – Peter Wood Feb 25 '13 at 13:49
  • Your `Player` class should have a copy constructor and an assignment operator. – molbdnilo Feb 25 '13 at 14:59

1 Answers1

0

I don't see why you make oss a member of Orbs. It is used in a way that is more appropriate for a local variable, so you should remove it:

class Orbs
{
public:
Orbs();
~Orbs();
void Render(sf::RenderWindow &target);
void UpdateText();

private:
Orb air_orbs, darkness_orbs, death_orbs, earth_orbs, electricity_orbs,
    fire_orbs, life_orbs, light_orbs, machine_orbs, metal_orbs, time_orbs,     water_orbs;
};

void Orbs::UpdateText()
{
    std::stringstream oss;
    oss << air_orbs.number;
    air_orbs.text.setString(oss.str());
}

Or with C++11:

void Orbs::UpdateText()
{
    air_orbs.text.setString(std::to_string(air_orbs.number));
}
  • Because i will need to Update it constantly, every update call of the program. i would then create and delete the member each time this way. – mickaelalliel Feb 25 '13 at 13:15
  • I doubt that the allocation/deallocation will be much overhead compared to the actual string conversion. – Nobody moving away from SE Feb 25 '13 at 13:16
  • The problem is with std::to_string is my number is an integer, there is no overload for int. – mickaelalliel Feb 25 '13 at 13:17
  • In [cppreference](http://en.cppreference.com/w/cpp/string/basic_string/to_string) the first entry is for `int`. Are we talking about the same function? – Nobody moving away from SE Feb 25 '13 at 13:19
  • Well i have 3 options, long double _Val, _Ulonglong _Val and _Longlong _Val :/ – mickaelalliel Feb 25 '13 at 13:21
  • Both, the IDE Code completion shows me 3 overloads, and when i compile using an integer it tells me Error 1 error C2668: 'std::to_string' : ambiguous call to overloaded function d:\dropbox\public\programming c++\testgame\testgame\orbs.cpp 146 1 TestGame – mickaelalliel Feb 25 '13 at 13:24
  • Maybe they assume you use atoi or another facility (boost lexical_cast). Anyway the solution with the local stringstream should work too. – Nobody moving away from SE Feb 25 '13 at 13:27
  • Using a local stringstream still causes the unhandled exception when closing the program.. I really have no idea why it does that though – mickaelalliel Feb 25 '13 at 13:29
  • The problem will certainly lie somewhere else and the presence of the stringstream only changes things, so that the error gets triggered. My guess is that there is an invalid pointer somewhere or a out of bounds read on an array/vector, but this could be anywhere in your code. – Nobody moving away from SE Feb 25 '13 at 13:40