Server(memory heap?) - SFML & cppdb

closed account (3q9hb7Xj)
So, I'm making a game using SFML and cppdb( http://cppcms.com/sql/cppdb/ - I use it in server code ). I have 2 C++ projects. One is for server, and one is for client. Client works perfectly... Client has only basic functions for now (login, register, reset pass)... When I use login in my client and try to login with this acc:
Email:test@test.com
Password:test
everything works perfectly.

But when I try to login with this account:
Email:starfighter898@gmail.com
Password:test
I get memory heap in server...
Its... really weird...

Here is the code(server):
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
#pragma region LOGIN
else if (m == "login")
{
	std::string password;
	std::string email;
	std::string ip;
	packet >> email >> password >> ip;
								
	if (priv::isValidEmailAddress(email.c_str()))
	{
		if (password.size() > 0 && password.size() < 51)
		{
			if (email.size() > 2 && email.size() < 255)
			{
				#pragma region EMAIL CHECK
				int existsemail = 0;
								
				cppdb::result r = sql << "SELECT * FROM users WHERE email=?" << email << cppdb::row;
				if (!r.empty())
					existsemail = 1;
				#pragma endregion
								
				if (existsemail == 1)
				{
					#pragma region PASSWORD CHECK
					int existsUser = 0;
								
					cppdb::result re = sql << "SELECT * FROM users WHERE email=? AND password=?" << email << password << cppdb::row;
					if (!r.empty())
						existsUser = 1;
					#pragma endregion
														
					if (existsUser == 1)
					{
						pl[i].Password = password;
						pl[i].Email = email;
						pl[i].Ip = ip;

						#pragma region GET DATA
						cppdb::result res = sql << "SELECT * FROM users WHERE email=?" << email << cppdb::row;

						if (!res.empty()) {
							pl[i].Username = r.get<std::string>("username");
							pl[i].HasChat = (r.get<int>("hasChat") == 1);
							pl[i].IsAdmin = (r.get<int>("isAdmin") == 1);
							pl[i].IsBanned = (r.get<int>("isBanned") == 1);
							pl[i].IsMod = (r.get<int>("isModerator") == 1);
							pl[i].PlId = r.get<std::string>("id");
						}
						#pragma endregion

						sql << "UPDATE users SET ip_login=? WHERE email=?" << ip << email << cppdb::exec;
						sf::Packet successMsgData;
						successMsgData << "login" << "success"/*WE LOG IN*/ << pl[i].Username << pl[i].HasChat <<
 pl[i].IsAdmin << pl[i].IsBanned << pl[i].IsMod << pl[i].PlId << email << password;
						pl[i].Send(successMsgData);
					}
					else
					{
						sf::Packet errEmailMsgData;
						errEmailMsgData << "login" << "err"/*ERROR OCCURED*/ << 6/*ERROR ID*/;
						pl[i].Send(errEmailMsgData);
					}
				}
				else
				{
					sf::Packet errEmailMsgData;
					errEmailMsgData << "login" << "err"/*ERROR OCCURED*/ << 5/*ERROR ID*/;
					pl[i].Send(errEmailMsgData);
				}
			}
			else
			{
				//Same as above(I did it to reduce code for you to read)
			}
		}
		else
		{
			//Same as above(I did it to reduce code for you to read)
		}
	}
	else
	{
		//Same as above(I did it to reduce code for you to read)
	}
}
#pragma endregion 
Last edited on
It's not clear what the pl stuff is.

What line does it crash on?
closed account (3q9hb7Xj)
Pl is vector of this Player struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Player
{
	sf::TcpSocket* m_socket;

	std::string Username;
	std::string Password;
	std::string Email;
	std::string Ip;
	std::string PlId;
	bool IsMod;
	bool IsAdmin;
	bool IsBanned;
	bool HasChat;

	void Send(sf::Packet data)
	{
		m_socket->send(data);
	}
	sf::Socket::Status Receive(sf::Packet& packet)
	{
		return m_socket->receive(packet);
	}
};

 
std::vector<Player> pl;


I'm not sure about line :/ It just shows a message box that says that I got heap in server.exe or in dll that I'm using... :(
Well... Here is the source(i removed some parts of code that are not needed... like handling the "register", "rpass", email sending...):
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
int main()
{
	cppdb::session sql("mysql:database=buildingblocks;user=root;password='';");

	sf::TcpListener listener;
	sf::SocketSelector selector;
	bool done = false;
	std::vector<Player> pl;

	//email code - removed

	listener.listen(2000);
	selector.add(listener);

	while (!done)
	{
		if (selector.wait())
		{
			if (selector.isReady(listener))
			{
				Player m_pl;
				sf::TcpSocket *socket = new sf::TcpSocket;
				listener.accept(*socket);
				m_pl.m_socket = socket;
				pl.push_back(m_pl);
				selector.add(*socket);
			}
			else
			{
				for (int i = 0; i < pl.size(); i++)
				{
					if (selector.isReady(*pl[i].m_socket))
					{
						sf::Packet packet, sendPacket;
						if (pl[i].Receive(packet) == sf::Socket::Done)
						{
							std::string m;
							packet >> m;
							
							if (m == "register")
							{
								/// Not needed
							}

							else if (m == "login")
							{
								/// I already showed this code
							}

							else if (m == "rpass")
							{
								/// Not needed
							}

							else
							{
								///Nothing
							}
						}
						if (pl[i].Receive(packet) == sf::Socket::Disconnected)
						{
							selector.remove(*pl[i].m_socket);
							pl[i].m_socket->disconnect();
							delete (pl[i].m_socket);
							pl.erase(pl.begin() + i);
						}
					}
				}
			}
		}
	}

	for (int i = 0; i < pl.size(); i++)
	{
		selector.remove(*pl[i].m_socket);
		pl[i].m_socket->disconnect();
		delete (pl[i].m_socket);
		pl.erase(pl.begin() + i);
	}
	return 0;
}


Please help :(...
Run it as a program in a debugger, the program will stop and break into the debugger when you hit the error.
closed account (3q9hb7Xj)
It doesnt crash/hit the error when I run it in debug mode... -.-
So, is the problem in my .dll files?
Last edited on
No. Are you building it the same way when you run it as a service and in the debugger? i.e. are you running them both as release?
closed account (3q9hb7Xj)
Huh...
First I built it in Visual C++ in release mode:
Tried to log in with that 'problematic' user....
Server crashed...

Then I built it in Visual C++ in debug mode:
Again tried to log in with that 'problematic' user...
Everything worked perfectly...

Sorry I'm not familiar with debuggers...?
Argh... Sorry :(
The debug heap has a different layout, it has space for diagnostic info.

I am asking you to run the release build in the debugger, not a debug build.

It looks like you've corrupted the heap somehow. Without understanding your code, all I do is work backward from the crash.
Last edited on
closed account (3q9hb7Xj)
Heh, I just fixed it!!! Thought, I didn't run it in debugger. I always wanted to replace cppdb with MySQL Connector C++ in my server code. And I just replaced it.
Also, I rewrote whole server code(to get rid of some other bugs too)... and it works perfectly now!!! :D

Thanks for help!
Last edited on
Topic archived. No new replies allowed.