problem with STL containers

Hi,

I've developing a socket-based network piece of software as hw. I ran into some problems using STL's map and vector.

What my program does is keeping a map <socket_id, vector <message *> > allMsg of messages directly bound to their specific socket. Everything work great, except when I receive a message and try to put it at the end of the vector, like this:

m = new message ();

... set m's data from the bytes received ...

allMsg [c].push_back (m)

Not working!

I've tried to get a local copy of the vector, like:

vector <message *> msg_queue = allMsg [c];
m = new message ();

... set m's data ...

msg_queue.push_back (m);

... now since msg_queue is local, copy it, member by member, to the allMsg [c]


Not working, too!

Any ideas guys? I've given up asking myself why
It seems like it *should* work. What kind of error do you get? (compile- or runtime? If it is less than 2 pages, it would be good to post the exact message...)
I don't receive any error message and for the first message, everything works. Let me just copy-paste the exact code snippets:

1. init
fromMessages [conn].clear (); //conn is the socket, fromMessages is the map

2. update
int active_connection = ( *i ).first;
vector <MESSAGE *> message_queue = ( *i ).second;
message_queue.clear ();


// ... code to check if I received something, using recv ...
MESSAGE* msg = new MESSAGE ();
memcpy ( msg, ( MESSAGE * ) buffer, sizeof ( MESSAGE ) );
message_queue.push_back ( msg );


3. update the map's vector from local vector

fromMessages[active_connection].clear ();
for (int j = 0; j < message_queue.size (); j++)
{
fromMessages [active_connection].push_back (message_queue [j]);
}
message_queue.clear ();


Here, normally, on socket k, I would have a vector with all messages received on that socket, which I don't.
Looks like the first method should work. If socket c doesn't exist in the map though you could have problems, try printing out the vector you get from allMsg[c] and make sure it contains data.
Here is some output from console, maybe it helps:

Examining existing IPs for this station: ...
Looking for the first IP v4 match... bound to 192.168.0.83
Starting server on 192.168.0.83 : 8800 ... server started
fromConnections.size () = 1 // I have connected to this ip from other host, so we have a vector with 1 entry
fromMessages.size () = 1 //same, i have a single entry in the map, altough the vector is empty, no messages yet
New message from 192.168.0.83 I've send a message, the code works for the first one
Message queue after incoming message: 1
Message queue:
Client -> #msg: 1
Running command: xxx

fromConnections.size () = 2 //Proof, now i'm connected from 2 hosts, you can also see below
fromMessages.size () = 2 // here
New message from 192.168.0.83 //And of course, the message from second host is intercepted, but i'm unable to save it anymore in the map

The weird this is that the map has 2 entries, meaning the keys are different (which is right, no overwrites). 2 vectors are inited, I just can't write inside them!! :))
Me Out
When you say it doesn't work, what do you mean? Are you getting a run-time error? Is it just not writing to the vector?
I don't have runtime errors since the code continues to work without problems. Not to mention compile errors since I am able to run the program.

Guys do you think it would be easier just to drop the pointer thing, like reconsidering my map to be
map<int, vector <message> > fromMessages ?
Last edited on
I've solved the problem guys... the problem was related to the fact that every = or * operation on the map (like (*iter).first) or vector <message> v = fromMessages [c], made a local copy no matter how i referenced the map.

The solution was to use the iterator directly, like:
(*i).second.clear () -> corect
(*i).second.push_back (x) -> corect
everything else got me into a dead end so ... thanks everybody for support :)
Topic archived. No new replies allowed.