emplace command in std::map

Nov 30, 2017 at 3:17am
I'm having segmentation faults when I add something to a std::map with emplace. I have a map

std::map<std::string, std::vector<uint8_t*>> mymap;

and I add values to it

std::string newstring("abcdefgh");
std::vector<uint8_t*> newvector(0);

then add it to the map

mymap.emplace(newstring, newvector); // sometimes get segfaults here

later I add the pointers after allocating memory for them

uint8_t *newpointer = new uint8_t[50];
uint8_t *otherpointer = new uint8_t[75];

(mymap[newstring]).push_back(newpointer); // sometimes get segfaults here
(mymap[newstring]).push_back(otherpointer);


Not sure why...In the debugger the segfault happens when emplace looks to see if the string I'm passing is unique such that string is being compare to an empty string

Thanks
Last edited on Nov 30, 2017 at 5:53am
Nov 30, 2017 at 3:47am
You aren't posting the actual code you use or the context in which that code appears, so your description is fairly useless for the purposes of determining what is happening to give you the results you're getting.

Distill your code down to a reasonably sized compilable snippet that reproduces the problem you are experiencing. Post it here or in a new thread along with as much information as you can supply.
Nov 30, 2017 at 4:31am
std::vector newvector(0);
the template parameter for std::vector<> is missing
Nov 30, 2017 at 5:58am
Yep sorry wasn't at home...here's the code. This is a testbench that allocates memory for a byte array, writes it to memory, they uses the map to keep track of the pointers to the array so they can be deleted as each part finishes so that memory can be recovered. I can't use a shared pointer because I'm writing the address and size into a software ring that hardware will then read to retrieve the information.

// issue packet to software ring
void jproducer::issue(std::shared_ptr<jpacket>& packet) {
// pointers for data
std::shared_ptr<jarray<uint8_t>> myin;

// retrieve the data from the packet
packet->get_input(myin);

// write data to memory
uint64_t iaddr;
uint64_t oaddr;

// tracking for memory
std::vector<uint8_t*> tmp(0);
m_mmu->emplace(packet->get_name(), tmp);

// write data to memory
iaddr = write(packet->get_name(), myin->get_ptr(), myin->get_size());

// write data to memory
oaddr = get_mem(packet->get_name(), packet->get_outlen());
packet->set_outaddr(oaddr);

// create the input ring entry
ringin myrin(iaddr,
myin->get_size(),
packet->get_stream(),
((((m_flows[packet->get_stream()])->get_dir() == ENCAP) ||
((m_flows[packet->get_stream()])->get_dir() == UPLINK)) ? true : false),
oaddr);

// issue the packet
m_iring->push(myrin);

// log issued packet
m_logger->print<jlogger::severity_type::info>("jproducer::issue() issuing packet=",
packet->get_name(),
" stream=",
packet->get_stream(),
" addr=",
HEXLOG(iaddr,16),
" size=",
myin->get_size(),
" oaddr=",
HEXLOG(oaddr,16));
}
Topic archived. No new replies allowed.