SFML community forums
Help => Network => Topic started by: Randati on October 02, 2010, 09:26:26 pm
-
I have a problem using sf::Packets with sf::UdpSocket::Receive(). Every time I call the function it allocates more and more memory until it hits 0.5 GB and crashes. Here's a minimal example.
#include <SFML/Network.hpp>
int main() {
sf::UdpSocket socket;
socket.SetBlocking(false);
socket.Bind(54354);
sf::IpAddress client_ip;
unsigned short client_port;
sf::Packet packet;
while (true) {
socket.Receive(packet, client_ip, client_port);
}
}
Without Packets it works just as it should. It doesn't allocate more memory with every call.
char buffer[1024];
std::size_t received = 0;
while (true) {
socket.Receive(buffer, sizeof(buffer), received, client_ip, client_port);
}
Internally sf::UdpSocket::Receive() grows the size of sf::UdpSocket::myPendingPacket.Data every time.
Am I doing something wrong here or is this maybe a bug?
I'm using the latest SFML 2.0 snapshot.
-
Yes, you're doing something wrong.
What is the differences between the two codes?
In the first one you use a 'dynamic size' container to receive data into. In the second one you use a static one.
That's why in the first example your App is taking more and more memory. Now you may wonder how to use Packet without this bad growing and growing thing. You have simple to clear it. Check out the tutorial or the doc to find the right function.
-
I can't find any function that would clear the socket.
The problem happens only in non-blocking mode when the socket is not receiving anything. Then UdpSocket::Receive returns NotReady. If it received something, it would return Done and clear itself.
Any tutorial or doc is not really helping. Maybe I'm looking from wrong places.
-
Not clear the socket but the packet. What happens if you use http://www.sfml-dev.org/documentation/2.0/classsf_1_1Packet.htm#3a7a30fb7a39e59df0cbd773756f60da after each receive ?
-
It doesn't help. The packet is empty already, because the socket is not receiving anything. Also UdpSocket::Receive() clears the packet before using it.
The packet's size is always 0, but UdpSocket::myPendingPacket.Data is growing.
-
How do you send data on the other side?
-
<snip>crossposting
-
I'm not sending anything yet. Only the server is running. It crashes in a couple seconds if nothing is send and I don't slow the loop at all.
OS is 64bit Windows 7. Not OS related though, same happens on Ubuntu.
-
Revision 1583 fixed this, thank you!