Error using std::string std::bad_alloc

1: Unhandled exception at at 0x75151D4D in Game.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x006BF2AC.

2: Unhandled exception at 0x6E6E51C4 (msvcr110.dll) in Game.exe: 0xC0000005: Access violation reading location 0x0000006C.

I'm trying to create a client/server program to chat using SFML networking libs.
Everything went fine untill for some reason this error started to pop up an hour ago.
I don't have a clue why it's there because everything worked fine before and I didn't change it.

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
bool Game::Login()
{	
	// Send username
	sf::Socket::Status *stat = new sf::Socket::Status(m_ClientPtr->sendName());

	// Act according to the response
	switch (*stat)
	{
	case sf::Socket::Done:		
		delete stat;
		return true;
		break;
	case sf::Socket::NotReady:
		delete stat;
		return false;
		break;
	case sf::Socket::Disconnected:
		delete stat;
		GAME_ENGINE->MessageBox("ERROR 004: Disconnected.");
		m_CurrentState = STATE::LOGIN;
		return false;
		break;
	case sf::Socket::Error:
		delete stat;
		GAME_ENGINE->MessageBox("ERROR 003: Failed to connect to the server.");
		m_CurrentState = STATE::LOGIN;
		return false;
		break;
	default:
		break;
	}	

	return false;
}


1
2
3
4
5
6
sf::Socket::Status Client::sendName()
{
	sf::Packet packet;
	packet<<INITIAL_NAME_DATA<<m_Name;  <-- here
	return m_Me->send(packet);
}
Last edited on
> sf::Socket::Status *stat = new sf::Socket::Status(m_ClientPtr->sendName());
http://www.cplusplus.com/forum/general/138037/#msg731921


http://www.eelis.net/iso-c++/testcase.xhtml (6)
A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.
Ow yeah, sorry I tried that as one of the solutions forgot to change it back.

Here is more of the code then.

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
121
122
123
124
125
126
127
void Game::CallAction(Caller* callerPtr)
{
	// Login
	if (callerPtr==m_BtnGoPtr)
	{
		if(Connect())		
		{
			if (Login())	
			{
				CheckUserName();
			}
		}
		
	}
}
bool Game::Connect()
{
	// Get name
	m_Name = ConvertString(m_TxtNamePtr->GetText());		

	// Start connection
	m_ClientPtr = new Client(m_Name);		
	sf::Socket::Status stat(m_ClientPtr->connect(IPADRESS,5005));
	
	// Act according to the response
	switch (stat)
	{
	case sf::Socket::Done:
		return true;
		break;
	case sf::Socket::NotReady:
		break;
	case sf::Socket::Disconnected:
		GAME_ENGINE->MessageBox("ERROR 002: Disconnected.");
		m_CurrentState = STATE::LOGIN;
		break;
	case sf::Socket::Error:
		GAME_ENGINE->MessageBox("ERROR 001: Failed to connect to the server.");
		m_CurrentState = STATE::LOGIN;
		break;
	default:
		break;
	}	

	return false;
}

bool Game::Login()
{	
	// Send username
	sf::Socket::Status stat(m_ClientPtr->sendName());

	// Act according to the response
	switch (stat)
	{
	case sf::Socket::Done:		
		return true;
		break;
	case sf::Socket::NotReady:
		return false;
		break;
	case sf::Socket::Disconnected:
		GAME_ENGINE->MessageBox("ERROR 004: Disconnected.");
		m_CurrentState = STATE::LOGIN;
		return false;
		break;
	case sf::Socket::Error:
		GAME_ENGINE->MessageBox("ERROR 003: Failed to connect to the server.");
		m_CurrentState = STATE::LOGIN;
		return false;
		break;
	default:
		break;
	}	

	return false;
}

bool Game::CheckUserName()
{
	// Store info in these
	std::string msg = "";
	PacketType ptype;

	// Get response on the username
	sf::Socket::Status stat (m_ClientPtr->receive(ptype,msg));
	switch (stat)
	{
	case sf::Socket::Done:
		// Check type
		if (ptype==SERVER_NAME_TAKEN)
		{
			GAME_ENGINE->MessageBox("ERROR 007: Name already taken.");
			m_CurrentState = STATE::LOGIN;
			return false;
		}
		else if (ptype==SERVER_FULL)
		{
			GAME_ENGINE->MessageBox("ERROR 008: Server full.");
			m_CurrentState = STATE::LOGIN;
			return false;
		}

		m_CurrentState = STATE::HOMESCREEN;
		GAME_ENGINE->MessageBox("SUCCES");

		return true;
		break;
	case sf::Socket::NotReady:
		return false;
		break;
	case sf::Socket::Disconnected:
		GAME_ENGINE->MessageBox("ERROR 005: Disconnected.");
		m_CurrentState = STATE::LOGIN;
		return false;
		break;
	case sf::Socket::Error:
		GAME_ENGINE->MessageBox("ERROR 006: Failed to connect to the server.");
		m_CurrentState = STATE::LOGIN;
		return false;
		break;
	default:
		break;
	} 		

	return false;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include "SFML/Network.hpp"
#include "PacketType.h"

class Client
{
public:
	Client(const std::string &name);
	~Client();

	sf::Socket::Status connect(const sf::IpAddress &IP, unsigned short port);
	sf::Socket::Status send(PacketType type, std::string &msg);
	sf::Socket::Status sendName();

	sf::Socket::Status receive(PacketType &ptype, std::string &msg);

private:

	std::string m_Name;
	sf::TcpSocket *m_Me;

	Client(const Client& yRef);									
	Client& operator=(const Client& yRef);	
};

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
#include "Client.h"
#include "PacketType.h"
#include <iostream>

Client::Client(const std::string &name):
	m_Name(name),
	m_Me(nullptr)
{
	m_Me = new sf::TcpSocket();
}

Client::~Client(void)
{
	delete m_Me;
}

sf::Socket::Status Client::connect(const sf::IpAddress &IP, unsigned short port)
{
	//connect to server
	sf::Socket::Status stat= m_Me->connect(IP, port);
	m_Me->setBlocking(false);
	return stat;
}

sf::Socket::Status Client::send(PacketType type, std::string &msg)
{
	sf::Packet packet;
	packet<<type<<msg;
	return m_Me->send(packet);
}

sf::Socket::Status Client::sendName()
{
	sf::Packet packet;
	packet<<INITIAL_NAME_DATA<<m_Name;
	return m_Me->send(packet);
}

sf::Socket::Status Client::receive(PacketType &ptype,std::string &msg)
{
	sf::Packet packet;
	sf::Socket::Status status=m_Me->receive(packet);
	//PacketType type;
	packet>>ptype;
	if(status==sf::Socket::Done)
	{
		std::cout<<msg<<"\n";
		packet>>msg;
	}
	return status;
}
Last edited on
UPDATE

I found a piece of the problem, not sure how to fix it now though.

WORKS:
1
2
3
4
5
6
sf::Socket::Status Client::sendName()
{
	sf::Packet packet;
	packet<<INITIAL_NAME_DATA<<"123";  <-- here
	return m_Me->send(packet);
}


DOES NOT WORK:
(m_Name = "123")
1
2
3
4
5
6
sf::Socket::Status Client::sendName()
{
	sf::Packet packet;
	packet<<INITIAL_NAME_DATA<<m_Name;  <-- here
	return m_Me->send(packet);
}
Last edited on
Silly me!
I looked way to far I guess, I've wasted an hour while the solution was just this.
(it worked before without this though.)
Sorry guys.

1
2
3
4
5
6
sf::Socket::Status Client::sendName()
{
	sf::Packet packet;
	packet<<INITIAL_NAME_DATA<<m_Name.c_str();  <-- here
	return m_Me->send(packet);
}


Very weird, I can fix the problems by adding .c_str() everywhere, I didn't need to do this earlier. mmmm
Last edited on
> Here is more of the code then
http://www.eelis.net/iso-c++/testcase.xhtml (6)
A testcase that does not reproduce the problem is useless.

Given that you've got a runtime error, at least give enough code to build your program.
As you are using several files, it would be nice if you could create a github project or upload a zip.


> while the solution was just this.
> (it worked before without this though.)
then perhaps that is not the solution
you may have invoked undefined behaviour. Run through a debugger and valgrind
Topic archived. No new replies allowed.