Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - mcphee.d

Pages: [1]
1
Network / sf::Packet.Clear() heap corruption?
« on: August 27, 2011, 06:05:16 am »
Hello,

I have this simple server code, taken pretty much directly from the selector tutorial:
Code: [Select]


void mv_server::listen_loop() {
while(1) {
if(selector.Wait()) {
if(selector.IsReady(listener)) {
sf::TcpSocket* client = new sf::TcpSocket();
if(listener.Accept(*client) == sf::Socket::Done) {
clients.push_back(client);
selector.Add(*client);
}
}
else {
for(std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); it++) {
sf::TcpSocket& client = **it;
if(selector.IsReady(client)) {
sf::Packet recv_packet;
sf::Packet send_packet;
if(client.Receive(recv_packet) == sf::Socket::Done) {
std::string got;
recv_packet >> got;
std::cout << "The client said: " << got << std::endl;
//packet.Clear();
send_packet << "Got yo message yo";
client.Send(send_packet);
}
}
}
}
}
}
}


This function is in an sf::thread which is called earlier.

Then, this simple client code:

Code: [Select]

if(e.Type == sf::Event::KeyPressed) {
if(e.Key.Code == sf::Keyboard::A) {
//send a message to our server
if(connected_to_dude) {
sf::Packet p;
p << "We pressed A";
int status = connection_dude.Send(p);
p.Clear();
connection_dude.Receive(p);
std::string got;
p >> got;
std::cout << "Server says " << got << std::endl;
std::cout << "Sent status: " << status << std::endl;
p.Clear();
} else {
std::cout << "connect first!" << std::endl;
}
}
}


Running this and triggering that event crashes the program, complaining of a heap corruption. If I remove that "p.Clear()" there, it doesn't crash. However, I now have to pop two items off the packet to get my server's response, right? So I do

Code: [Select]

std::string got;
std::string got2;
p >> got >> got2;


...and it crashes with the same heap corruption.

Am I missing something? I left out various instantiations to save on space; if you need any more info let me know!

Pages: [1]