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

Author Topic: sf::packet and strings  (Read 26774 times)

0 Members and 1 Guest are viewing this topic.

Regen

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
sf::packet and strings
« on: July 16, 2008, 09:45:08 am »
hi! I have made a class called "sensor" with 2 floats and a std::string. Now i want to pack those in to a packet called Temp, my operators look like this:
sf::Packet& operator <<(sf::Packet& Pack, const Sensor& C)
{
   return Pack << C.PosX << C.PosY << C.Target_type;
}
When i run the code i get the memoryerror:
"Unhandled exception at 0x7c812a5b in SFML-Server.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fb0c.."

When i modify the operator and delete the last variable (the string Target_type), it works smooth. I have read some and as far as i understand it should be possible to pack a string to a packet?.....right?
Why can't things just work?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::packet and strings
« Reply #1 on: July 16, 2008, 09:57:11 am »
And if you use sf::Packet::Append
 like this :

Code: [Select]
Append(C.Target_type.c_str(), C.Target_type.size() * sizeof(std::string::value_type));

does it work?
(You may use a char* to get again the data I think.)

Can you use your dbg to get more informations on the bad_alloc?
SFML / OS X developer

Regen

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
sf::packet and strings
« Reply #2 on: July 16, 2008, 10:25:34 am »
for some reason
Code: [Select]
Append(C.Target_type.c_str(), C.Target_type.size() * sizeof(std::string::value_type));

 dont work, it does'nt understand that Target_type is a member of Sensor :?
And the only more information i can get is that the program stopps after the
Code: [Select]
return Pack << C.PosX << C.PosY << C.Target_Type;

I'm not verry good at debuging...
Why can't things just work?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::packet and strings
« Reply #3 on: July 16, 2008, 10:43:32 am »
Two things :

1. Make sure you link to the debug SFML libraries (-d) in debug mode, and to the release libraries in release mode.

2. Is the string empty ?
Laurent Gomila - SFML developer

Regen

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
sf::packet and strings
« Reply #4 on: July 16, 2008, 10:53:38 am »
Quote
1. Make sure you link to the debug SFML libraries (-d) in debug mode, and to the release libraries in release mode.

I realised that i was linking to the release libs and changed that and crossed my fingers, the compilation went fine but when trying to run it, i got a strange error message:
"Debug Assertion Failed!
Program: ...
File: d:\programmes\visual studio 2005\vc\include\vector
Line: 756

Expression: vector subscript out of range

for information on how.....bla bla"

I dont even have a d:\programmes! :O

Quote
2. Is the string empty ?

No, in the class Sensors deafult constructor it is filled with a random string. (the floats are filled aswell)
Why can't things just work?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::packet and strings
« Reply #5 on: July 16, 2008, 11:25:36 am »
It seems the assertion is thrown from SFML source code, that's weird.

Can you try to debug and give more informations about the crash ? Or, if you can't, can you provide a minimal sample code which reproduces the problem ?

Thank you :)
Laurent Gomila - SFML developer

Regen

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
sf::packet and strings
« Reply #6 on: July 16, 2008, 11:38:58 am »
Code: [Select]
#include <SFML/Network.hpp>
#include <string>

class thing{
public:
std::string text;
void THING(){this->text = "Test";}
};

int main(){
sf::Packet P;
thing T;
P << T.text;

return 0;
}


This simple code reproduces my errors, the memory error when not using -d libs, and the wierd D:\programmes error when having the right libs
Why can't things just work?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::packet and strings
« Reply #7 on: July 16, 2008, 11:57:41 am »
Here your string is empty, because you don't call thing::THING.

Perhaps you confuse thing::THING with thing::thing.  thing::thing is the builder of the class.

Try this :
Code: [Select]

#include <SFML/Network.hpp>
#include <string>

class thing{
public:
   std::string text;
   thing(void) : text("Hello World") { /* Nothing else */ }
};

int main(){
   sf::Packet P;
   thing T;
   P << T.text;

   return 0;
}


PS : in English, how do we say "builder" for a class (in programming)? Thanks.
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::packet and strings
« Reply #8 on: July 16, 2008, 12:38:02 pm »
There might be problems with empty strings and packets (I discovered it today), so make sure the bug appears with non-empty strings.

Quote
PS : in English, how do we say "builder" for a class (in programming)? Thanks.

Same as in french : constructor. "Destructeur" is also destructor ;)
Laurent Gomila - SFML developer

Regen

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
sf::packet and strings
« Reply #9 on: July 16, 2008, 12:55:57 pm »
Your code worked, so I modified mine so the string was'nt empty anymore, and now it runs smooth, thanks!

p.s. laurent, it would however be nice if it still would be possible to send empty strings in sf::packet?

btw thanks guys! :)
Why can't things just work?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::packet and strings
« Reply #10 on: July 16, 2008, 01:08:13 pm »
Quote
p.s. laurent, it would however be nice if it still would be possible to send empty strings in sf::packet?

Of course, this is a bug I'm going to fix asap.
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::packet and strings
« Reply #11 on: July 16, 2008, 01:10:56 pm »
Quote from: "Laurent"

Quote
PS : in English, how do we say "builder" for a class (in programming)? Thanks.

Same as in french : constructor. "Destructeur" is also destructor ;)
Thanks
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::packet and strings
« Reply #12 on: July 16, 2008, 01:21:04 pm »
I fixed the bug, let me know if everything's ok now.
Laurent Gomila - SFML developer

Regen

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
sf::packet and strings
« Reply #13 on: July 16, 2008, 01:51:41 pm »
I am having the same problem with it yet:(
Why can't things just work?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::packet and strings
« Reply #14 on: July 16, 2008, 02:41:11 pm »
You mean, you got the lastest sources from SVN and recompiled SFML, and it still crashes ?
Laurent Gomila - SFML developer