Weird problem with Maps & OSC

Hi all,

So I made this bit of software on an installation that sends out osc messages to multiple ports, but storing osc objects in a map type list. It works for me, and on the original pc, but now they replaced the pc and it doesnt work anymore.

here is my definition

  
  
map <string, ofxOscSender> oscSenders;  
  

That way I can call rather than an array index, but a string, in my case the port is used as the index, so…

  
  
oscSenders["192.168.1.3"].sendBundle(..)  
  

here is the code where i make a new ofxOscSender and add it to the map

  
  
ofxOscSender tempSender;  
tempSender.setup( host, sendPort );  
oscSenders.insert( make_pair( host, tempSender) );  
  

It is crashing on the line:
oscSenders.insert( make_pair( host, tempSender) );

I have also tried oscSenders.insert( pair<string, ofxOscSender>( host, tempSender) );

Does anyone have any ideas? Thanks

Hi,

In my c++ book, talks about make_pair() but discourages that use.

Instead you could just write

  
ofxOscSender tempSender;  
tempSender.setup( host, sendPort );  
oscSenders[host] = tempSender;  

it’ll automatically create a new pair in your map.

hope the book tell the truth, but I use this in projects and seems to work fine.

And after reading a bit more, it appears that the only interest for calling the insert function is giving you back a pair with <position, indic>
position = the iterator of your new pair
indic = bool that tell you if insert succeed

If you doesn’t use them, prefer the direct way.

sorry for weird english.

Does calling

oscSenders[host] = tempSender;

add a new item into the map list, or would i get a memory error? normally with arrays you need an insert / push function to put stuff in, i.e.

names[3] = {“chris”, “simon”, “ben” }

names[10] = “i crash”

wouldnt work

yes you’re right.

But it’s not an array, nor a vector, map can do it.
it could be dangerous also, cause you never get out of bound error, because it inserts a new pair when you want access for new key.
In that way, i assume, it’s better to work with map of pointer.

anyway, the = does the trick.

but your former code is okay, maybe issue is somewhere esle

with this:

  
oscSenders.insert( make_pair( host, tempSender) );  

you’re indeed creating a copy of your original sender, the problem can be in the copy constructor not managing memory well in the windows version.

the way to go when inserting an object in a collection is to insert it first and initialize it later.

like:

  
oscSenders["ip"].setup(host,port);  

as pierrre says doing this:

  
oscSenders["ip"]  

is creating an instance so you dont need to create it first. that can be a problem though if “ip” doesn’t exist yet, and you are just trying to access the value.

the way to go in that cases is to try to find it before accesing it, like:

  
if(oscSenders.find("ip")!=oscSenders.end()){  
    oscSenders["ip"]...