Aug 20, 2014 at 4:57pm UTC
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 Aug 20, 2014 at 4:58pm UTC
Aug 20, 2014 at 5:21pm UTC
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 Aug 20, 2014 at 5:25pm UTC
Aug 20, 2014 at 5:33pm UTC
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 Aug 20, 2014 at 5:33pm UTC
Aug 20, 2014 at 5:36pm UTC
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 Aug 20, 2014 at 5:39pm UTC