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

Author Topic: sf::Packet.Clear() heap corruption?  (Read 4962 times)

0 Members and 1 Guest are viewing this topic.

mcphee.d

  • Newbie
  • *
  • Posts: 7
    • View Profile
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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Packet.Clear() heap corruption?
« Reply #1 on: August 27, 2011, 09:47:33 am »
Hi

Could you please provide a minimal and complete example that reproduces the problem? So that I can test and debug it.
Laurent Gomila - SFML developer

mcphee.d

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Packet.Clear() heap corruption?
« Reply #2 on: August 29, 2011, 02:47:48 am »
Hello! Sorry for the delay.

mv_server.h:
Code: [Select]

#include "stdafx.h"

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

private:
sf::TcpListener listener;
void listen_loop();
sf::Thread* Thread;
std::vector<sf::TcpSocket*> clients;
sf::SocketSelector selector;
};


mv_server.cpp:
Code: [Select]


#include "stdafx.h"

mv_server::mv_server() {
//initialize connection tcp listener
std::cout << "Initializing tcp listener ..." << std::endl;
listener.Listen(55001);
//boost::thread worker_thread(boost::bind(&mv_server::listen_loop,this));
selector.Add(listener);
Thread = new sf::Thread(&mv_server::listen_loop, this);
Thread->Launch();
}

mv_server::~mv_server() {

}

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);
}
}
}
}
}
}
}


mv_main.cpp:
Code: [Select]

int main(array<System::String ^> ^args)
{
sf::TcpSocket connection_dude;
bool connected_to_dude = false;
sf::RenderWindow menu_win(sf::VideoMode(400, 400, 32), "MV Menu");
//fork off a server
mv_server *listener = new mv_server();
//connect to it for a one-off test
//NOTE: I used my WAN ip here; insert your own or localhost or something
int status = connection_dude.Connect("xx.xxx.xxx.xxx", 55001);
connected_to_dude = true;

while(menu_win.IsOpened()) {
sf::Event e;
while(menu_win.PollEvent(e)) {
//close
if(e.Type == sf::Event::Closed) { menu_win.Close(); }
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;
}
}
}
           }
}
} //end main


stdafx.h:
Code: [Select]

#pragma once

#include <iostream>
#include <stdlib.h>

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

#include "mv_server.h"


Some details, if they matter --
I'm using VS2008, the latest (well, as of a couple weeks ago) SFML build from github, and my OS is Windows 7 64-bit. I'm building this in Debug configuration.

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Packet.Clear() heap corruption?
« Reply #3 on: August 29, 2011, 08:14:15 am »
This is C++/CLI. Have you tried it in pure C++ (this code doesn't use managed features, so it's just a matter of changing one project option and the prototype of main())?
Laurent Gomila - SFML developer

mcphee.d

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Packet.Clear() heap corruption?
« Reply #4 on: August 29, 2011, 01:55:12 pm »
I haven't tried that -- what option are you referring to?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Packet.Clear() heap corruption?
« Reply #5 on: August 29, 2011, 02:04:00 pm »
I don't know. Maybe it's the "Common Language Runtime support" option in "General". It must be set to "No Common Language Runtime support" for native C++.

But is there a reason why you use C++/CLI? Many beginners don't know what it is and don't know that this is not C++ ;)
Laurent Gomila - SFML developer

mcphee.d

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Packet.Clear() heap corruption?
« Reply #6 on: August 29, 2011, 03:56:15 pm »
There isn't a reason -- I didn't even know that was a ... thing! Hehe. When setting up the project, I guess I must have chose it.

I'll try turning CLR off, but what's the difference between this and "native C++"? You're right -- as far as I can tell I've just been writing C++. I didn't know I wasn't ... really writing C++? I'm confused lol.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Packet.Clear() heap corruption?
« Reply #7 on: August 29, 2011, 04:09:42 pm »
C++/CLI is a language created by Microsoft to make a bridge between native C++ and the .Net world. It's a kind of managed C++, with it you can write .Net programs in (extended) C++ while still being compatible with pure C++ source codes and libraries.
Laurent Gomila - SFML developer

mcphee.d

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Packet.Clear() heap corruption?
« Reply #8 on: August 29, 2011, 06:47:44 pm »
I see! Thanks for the explanation.

Okay, well - I changed it to non-clr (and took 'public' out of 'public class', etc), but it still produces the same error.

mcphee.d

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Packet.Clear() heap corruption?
« Reply #9 on: September 05, 2011, 07:16:56 pm »
So we changed the
Code: [Select]
std::string got
to a
Code: [Select]
char got[128]
and now it works fine.

I'm not sure why std::string would crash it in such a way, but apparently it did!