Initializing boost asio variables in a state program

There's this problem that I took too much time trying to figure out.

The problem is that I am trying to initialize this chunk of code without it starting at the main menu in my state program:
1
2
3
4
5
6
7
8
9
10
11
boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1], argv[2]);
tcp::resolver::iterator iterator = resolver.resolve(query);

chat_client c(io_service, iterator);

boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

char line[chat_message::max_body_length + 1];


This code came from a example at the boost example list: http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/example/chat/chat_client.cpp

I want the networking to start when the multiplayer state initializes.

This is how my program basically looks like:

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
//Main.cpp
main()
{
 if (!quit)
 {
  while( handleing_events )
  {
   CurrentState->handleEvents();
  }
  CurrentState->Logic();

  checks_if_the_state_changed();

  CurrentState->Render();
 }
 close stuff
}

//States.cpp
#include states.h

...

//I initialize everything right here

Multiplayer::Multiplayer()
{...}
Multiplayer::handleEvents()
{
 //I need access to chat_client c
}
Multiplayer::Logic()
{...}
Multiplayer::Render()
{...}
Multiplayer::~Multiplayer()
{
 //I need access to chat_client c & boost::thread t
}

I tried to put the stuff in extern at states.h, and initialize it at Multiplayer(), but in handleEvents & ~Multiplayer, it says "c" is not defined...
Sorry that I can't give anything to compile, I have < 10 000 lines of code :/

-Explain it to me like a 5 year old.
If it is alright for the networking to end every time you exit the 'Multiplayer' state, setting it as a local variable will cause it to be initialized upon the construction of a 'Multiplayer' instance. Alternately, you could have 'c' as a pointer (preferably std::unique_ptr or the boost version) and then simply dynamically allocate it, causing it to start when you need to. When you are done you can either just let the pointer fall out of scope (assuming its a unique_ptr) or manually deallocate it, to stop it consuming resources.

For defining it extern, all that does is say that it is defined in another compilation unit - you still need to have it defined somewhere (and thus initialized) at namespace scope. Though, another option would be if chat_client has the option for a default constructor and then an initializer, which would be easier than the pointer approach.

Also, I think you meant "> 10000 lines of code" :)
Well.. I make the un allocated pointer as a global variable..
std::unique_ptr<chat_client> c;

And then I made it allocate in the server state...
std::unique_ptr<chat_client> c(new chat_client( io_service, iterator));

I start the program and yep, it did not initialize...
but when I start the server, it also wont initialize,
and when I try to send a message the game would crash D:

Am I doing something wrong?
Apparently it seems to be a problem coming the function called write(). It would normally work if I constructed it as not a pointer, but when I molded it to the pointer function operator:
c.write(...); -> c->write(...);

It will just crash when it tries to open it...

And all this function contains is this...
1
2
3
4
void write(const chat_message& msg)
  {
    io_service_.post(boost::bind(&chat_client::do_write, this, msg));
  }


Is this a known mistake :/
bump?
Dammit guys... You could've explained to me I had to do:
if(c) c->write(...);

Its was so simple... Why wasn't there an answer way sooner ;w;
Oh sorry, I only just checked my 'My Topics' again... well, now that you've spent so much time on this at least you won't forget it. Though, partly the reason was you didn't give us the context to the call, so we didn't know that you weren't checking the validity of the pointer. Just for future reference.
I don't want to bump this but...
The validity wasn't the problem...
It seemed like at the time when I did that it made it compiled without errors or run time problems... but it didn't actually work.
I found the real solution shortly after:
1
2
3
4
5
6
//Define the undefined :O
  std::unique_ptr<chat_client>   cc(new chat_client( io_service, iterator)) ;
  c = std::move(cc);

  std::unique_ptr <boost::thread> tt( new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service)));
  t = std::move(tt);

:/
you don't need to move it, just reset: c.reset(new chat_client( io_service, iterator)) ;
Topic archived. No new replies allowed.