unordered_map of shared_ptrs breaks C++ program - c++

I use an unordered_map in my current C++ project and have the following problem:
When I insert a pair of objects into the unordered_map, the programm breaks and Windows shows me it's "[...].exe has stopped working", without giving me any information on the console (cmd). Some example code:
#include <unordered_map>
#include <network/server/NetPlayer.h>
#include <gamemodel/Player.h>
int main(int argc, char **argv) {
NetGame game;
boost::asio::io_service io_service;
NetPlayerPtr net(new NetPlayer(io_service, game));
PlayerPtr player(new Player);
std::unordered_map<PlayerPtr, NetPlayerPtr> player_map;
// Here it breaks:
player_map[player] = net;
return 0;
}
What I already tried:
I tried wrapping the line with a try-catch, but without success.
Details about the code:
NetPlayerPtr and PlayerPtr are boost::shared_ptr objects, the former contains some boost::asio objects like io_service and socket, the latter contains several custom objects.
I'm compiling with MinGW gcc with C++11 enabled on a 64bit Windows.
If more details are needed, please ask.

Okay, let's look at the code you linked to:
namespace std
{
template<>
class hash<Player>
{
public:
size_t operator()(const Player &p) const
{
// Hash using boost::uuids::uuid of Player
boost::hash<boost::uuids::uuid> hasher;
return hasher(p.id);
}
};
template<>
class hash<PlayerPtr>
{
public:
size_t operator()(const PlayerPtr &p) const
{
return hash<PlayerPtr>()(p); // infinite recursion
}
};
}
You have an inifinite recursion in your hash<PlayerPtr>::operator(). What you probably want is:
return hash<Player>()(*p);
or:
return hash<Player*>()(p->get());
depending on whether you want to identify the player by its internal id or its address.

Related

Cannot push_back() a struct into an std::vector<std::shared_ptr<theStruct>> theVector

Hopefully my title isn't too confusing. I'm trying to write a sound manager for my game using SFML. I'm trying to replace my new/delete with the "smart pointer" std::shared_ptr. This is what I have so far.
/* SoundManager.h */
#ifndef SOUNDMANAGER_H
#define SOUNDMANAGER_H
#include <SFML/Audio.hpp>
#include <string>
#include <memory>
class SoundManager
{
public:
~SoundManager();
struct jteSound
{
sf::Sound snd;
sf::SoundBuffer sndBuffer;
std::string name;
};
//Load a new sound from path and store it in audio bank bnk.
//Banks are simply std::vectors of type jteSound.
void registerNewSound(std::vector<std::shared_ptr<jteSound>> &bnk, std::string path, std::string sndName);
//Footsteps bank
std::vector<std::shared_ptr<jteSound>> bnkFootsteps;
};
#endif // SOUNDMANAGER_H
/* SoundManager.cpp */
#include "SoundManager.h"
#include <stdlib.h>
SoundManager::~SoundManager()
{
/*
//Cleanup each sound bank that we use.
for (std::vector<jteSound*>::iterator it = bnkFootsteps.begin(); it != bnkFootsteps.end(); ++it) {
delete *it;
}
*/
}
void SoundManager::registerNewSound(std::vector<std::shared_ptr<jteSound>> &bnk, std::string path, std::string sndName)
{
static int counter = 0;
for (int i = counter; counter <i+1; counter++) {
bnk.push_back(jteSound);
bnk[i]->name = sndName;
bnk[i]->sndBuffer.loadFromFile(path);
bnk[i]->snd.setBuffer(bnk[i]->sndBuffer);
}
}
bnk.push_back(jteSound); gives me a compiler error. If I remove the line, the program compiles, but crashes. I have tried things like emplace_back() or jteSound* or new jteSound, but nothing works. I always get a lengthy compiler error or immediate runtime crash. When I use regular pointers and new/delete, see https://bpaste.net/show/fa684f2f2d5e and https://bpaste.net/show/c74ac701ce7a, the code works as expected. Any thoughts appreciated!
The type of the elements inside your std::vector is std::shared_ptr<jteSound> which means that std::vector::push_back will accept only instances of that type.
To make your code work you have two options. The first is using std::make_shared helper function as follows:
bnk.push_back(std::make_shared<jteSound>());
// the equivalent counterpart is:
bnk.push_back(std::shared_ptr<jteSound>(new jteSound));
The second is using std::vector::emplace as follows:
bnk.emplace(bnk.end(), new jteSound);
As the comments below warn, using the second option is risky because it can cause memory leak when the new jteSound succeeds but the std::vector::emplace has to reallocate memory and fails.

I am trying to make a hashmap of of string functions

I am trying to make a map which stores a string as an identifier and a function that returns a string i have tried typedef but i kept running into problems because i couldn't convert my typedef string (command)() to a regular string i have also tried map commands but it gives me an expression expected error but it does work if i replace string with int. Does anybody know a way of doing this? This is what my code looks like
#include "iostream"
#include <map>
#include <functional>
using namespace std;
class GameController {
public:
void inputReader();
private:
bool gameOver = false;
map<string,string(*)()> commands;//Does not work
//commands
string commandReader(string* inputCommand);
void initCommands();
//both
char* end();
string run();
//while attacking
string attack();
string usePotion();
string useItem();
//while in room
string engage();
string searchRoom();
string rest();
string checkBag();
string checkMap();
string checkStats();
//string save();
};
#endif //ROGUE_GAMECONTROLLER_H
#include "GameController.h"
GameController::GameController(){
initCommands();
}
void GameController::inputReader() {
while (!gameOver){
string x;
getline(cin,x);
cout << commandReader(&x) << endl;
}
}
string GameController::commandReader(string *inputCommand) {
for (map<string,string>::iterator it = commands.begin(); it!=commands.end(); ++it)
{
if(it->first == *inputCommand)
{
return it->second;
}
}
return "Computer says no type help for commands";
}
void GameController::initCommands() {
commands["end"] = end;
//while attacking
commands["run"] = run;
commands["attack"] = attack;
commands["use potion"] = usePotion;
commands["use item"] = useItem;
//while in room
commands["engage"] = engage;//TODO
commands["search"] = searchRoom;
commands["rest"] = rest;
commands["check bag"] = checkBag;
commands["map"] = checkMap;
commands["stats"] = checkStats;
}
This question is tagged C++11, so here's a concise example which uses unordered_map (a real hash map, unlike std::map which my STL reference says is commonly implemented using binary search trees), and std::function.
#include <iostream>
#include <functional>
#include <string>
#include <unordered_map>
std::string foo()
{
return "foo!";
}
struct MyClass
{
static std::string bar()
{ return "bar!"; }
std::string FizzBuzz() const
{ return "FizzBuzz!"; }
std::string operator()() const
{ return "Myclass!"; }
};
int main(int argc, char **argv)
{
MyClass mc;
std::unordered_map<std::string, std::function<std::string()>> commands;
commands["myfoo"] = foo;
commands["mybar"] = MyClass::bar;
commands["myfb"] = std::bind(&MyClass::FizzBuzz, mc);
commands["myclass"] = mc;
for( const auto &f : commands)
std::cout << f.second() << std::endl;
std::cout << commands["myfoo"]() << std::endl;
return 0;
}
Pointers to member functions is not like pointers to free functions or even static methods. For one thing all member functions have a hidden this pointer in the function parameters that makes all of this object magic work.
Going through step by step:
First, define a helper:
typedef string (GameController::*funcp)();
This defines type funcp which represents a pointer to a member function of GameController (to partly take care of the this problem) that takes no parameters and returns string
Then, modify your map to use funcp
map<string, funcp> commands;
Then you have to change the assignment of the member functions a bit to be brutally explicit that it is a pointer and a member of GameController
commands["end"] = &GameController::end;
You can also save yourself some runtime trouble and use an initializer list here rather than a function and a map in every single GameController object. That'll take a bit of extra explaining and I have to be on the move in a few minutes. Sorry about that. A static map with static initializing really is better and worth your time researching, though.
The next bit I stole from the C++ Super FAQ. Read this link. Worth reading all of it, because it heads off a lot of the question you will have.
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
This makes calling the function awesomely easy.
return CALL_MEMBER_FN(*this, it->second)();
And that should about do it for you.
edit:
Tweej demonstrates the generally better way to do this, std::function and std::bind, in their answer. Since I'm advocating the ancient ways, I'd like to explain why.
Two reasons: one is tunnel vision directly answering OP's question.
The second is With the ancient ways I could easily make commands static and save having to create a new copy of commands for every instance of GameController. When using std::bind, you have to have the bound object, and that ruins the static idea.
Poking around at the idea of just using std::function seems to have born fruit and rendered obsolete the ancient ways. gone is the CALL_MEMBER_FN macro. Gone is the funcp typedef
The map is now defined as static, what I was aiming for the the old-pre C++11 approach. Note the funcp typedef is replaced by a function that takes a pointer to GameController to supply this.
static map<string, std::function<string(GameController*)>> commands;
And the map is now rigged to use a static initializer list. No function required. This initializer needs to sit outside the class definition because... I'm not sure why. I think this is changed in C++14.
map<string, std::function<string(GameController*)>> GameController::commands
{
{"end", &GameController::end},
{"run", &GameController::run},
{"attack", &GameController::attack},
{"use potion", &GameController::usePotion},
{"use item", &GameController::useItem},
{"engage", &GameController::engage},
{"search", &GameController::searchRoom},
{"rest", &GameController::rest},
{"check bag", &GameController::checkBag},
{"map", &GameController::checkMap},
{"stats", &GameController::checkStats}
};
The map is initialized once and only once. All GameControllers will use the same commands, so the constructor is really dumb
GameController::GameController()
{
// init function is gone
}
Command reader gets a big rip-up, mostly because the point of a map is you can search it by the key. So I search for the key rather than iterating. The function call is now obvious and dead simple:
string GameController::commandReader(const string &inputCommand)
{
map<string, std::function<string(GameController*)>>::iterator found = commands.find(inputCommand);
if (found != commands.end())
{
return found->second(this);
}
return "Computer says no type help for commands";
}

static unordered_map is erased when putting into different compilation unit in XCode

I have a static unordered_map in my class C. I experience difference in behaviour if I put my class definition and declaration in different files from the file containing function main.
The thing is that I observed that if the class C is in the same compilation unit as function main, all is well, I see only once the text "new string created: c". However if I split my code into three files (see the listing below), I see "new string created: c" twice which means that my static unordered_map is wiped right before entering main.
My question would be: why does this happen? (The difference only happens when compiling with Apple LLVM compiler 4.1. I have tested it with g++4.7 -std=c++11 and the split code works out just fine.)
Thanks in advance for any ideas!
// would go to My_header.h
#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;
class C{
public:
C(const string & s);
private:
static unordered_map<string, string*> m;
string *name;
};
// would go to My_code.cpp
// (when separated, add #include "My_header.h")
unordered_map<string, string*> C::m;
C::C(const string & s):
name(NULL)
{
string*& rs = m[s];
if(rs)
{
name = rs;
}
else
{
cout<<"new string created: "<<s<<endl;
rs = name = new string(s);
}
}
// would go to main.cpp
// (when separated, add #include "My_header.h")
C c("c");
int main(int argc, const char * argv[])
{
cout << "main" << endl;
C c1("c");
}
The order of initialization of global objects is defined only within one translation unit. Between different translation the order isn't guaranteed. Thus, you probably see behavior resulting from the std::unordered_map being accessed before it is constructed.
The way to avoid these problems is to not use global objects, of course. If you realky need to use a global object it is best to wrap the object by a function. This way it is guaranteed that the object is constructed the first time it is accessed. With C++ 2011 the construction is even thread-safe:
T& global() {
static T rc;
return rc;
}
Thanks, guys! Following Dietmar's advice, I did this:
class C{
//...
private:
static unordered_map<string, string*>& m();
};
unordered_map<string, string*>& C::m()
{
static unordered_map<string, string*> m;
return m;
}
and then I kept referring to m(). It is strange that it did not happen before. I guess I got lucky. But then, this should be a case for a warning message, shouldn't it?
To avoid mistakes like this I will use the following macros to declare and define static variables:
/// Use this macro in classes to declare static variables
#define DECLARE_STATIC(type, name) static type& name();
/// Use this macro in definition files to define static variables
#define DEFINE_STATIC(type, name) type& name(){static type local; return local;}
Usage in this case:
class C{
//...
private:
DECLARE_STATIC(unordered_map<string, string*>, m);
}
DEFINE_STATIC(unordered_map<string, string*>, m)

const_string lib issue

I'm trying const_string lib that looks not bad, but it crashes at runtime with access violation(atomic_count, operator++()). The test code:
#include <boost/const_string/const_string.hpp>
#include <boost/const_string/concatenation.hpp>
typedef boost::const_string<wchar_t> wcstring;
class Test
{
private:
const wcstring &s1;
const wcstring &s2;
public:
Test()
: s1(L"")
, s2(L"")
{
}
const wcstring &GetS1()
{
return s1;
}
const wcstring &GetS2()
{
return s2;
}
};
Test t;
int _tmain(int argc, _TCHAR* argv[])
{
//Test t;
wcstring t1 = t.GetS1(); // crashes here
wcstring t2 = t.GetS2();
return 0;
}
It crashes only if t is global. If I move declaration into main(), it's ok.
System: VS 2010, boost v. 1.47.0
The question: Am I doing something wrong or is it problem of library / compiler?
Can someone recommend a more stable implementation of immutable strings for C++?
Your instance of Test has initialized its reference data members as references to temporaries created from the literals L"".
Oops. The temporaries no longer exist by the time you try to use one of them in the copy constructor of wcstring at the line that crashes, so your references don't refer to anything.
I think boost::const_string should pretty much always be used by value, that's what it's for.

std::map fails to insert objects

A program I'm making requires access pools of various types of resources, but only accessible to a few classes. The resource pool is also its own class. As such, I decided to use a static class for the resource pool and have others access it this way.
But I am running into a strange problem with adding resources to the pool. Each pool is represented as an std::map, and after insertion, the map still remains empty. I am guessing this has something to do with the map being inside a static class. Is this why maps don't work as intended here?
Relevant code of Resource Pool
class ResourcePool {
private:
static ResourcePool m_ResourcePool;
public:
ResourcePool();
~ResourcePool();
static ResourcePool* Instance() { return &m_ResourcePool; }
// Where textures are stored. ci_less is for case comparison of names
std::map <std::string, TextureResource, ci_less> Textures;
TextureResource* getTexture(std::string handle);
};
Relevant code of how it's used
Scene::Scene() {
Assets = ResourcePool::Instance();
}
TextureResource* Scene::add(std::string handle, TextureResource Texture) {
// Insertion fails
Assets->Textures.insert(std::make_pair(handle + "_tex", Texture));
// I use this line to debug, the map still shows up empty
unsigned size = Assets->Textures.size();
// look up that texture by its name
return Assets->getTexture(handle);
}
The result is, add returns NULL since nothing is found. This program doesn't crash since I'm not doing anything with the texture, just testing out the insertion for now.
Since the question doesn't contain a proper compilable program, I have created one:
#include <string>
#include <map>
#include <iostream>
class ResourcePool {
private:
static ResourcePool m_ResourcePool;
public:
static ResourcePool* Instance() { return &m_ResourcePool; }
std::map <std::string, std::string> Textures;
std::string getTexture(std::string handle) { return Textures[handle]; }
};
ResourcePool ResourcePool::m_ResourcePool;
int main()
{
ResourcePool* Assets = ResourcePool::Instance();
Assets->Textures.insert(std::make_pair("test_tex", "texture"));
std::cout << Assets->Textures.size() << std::endl;
std::cout << Assets->getTexture("test_tex") << std::endl;
return 0;
}
This works on my machine as expected using linux, g++ Debian 4.3.2-1.1.
I wonder if it works for the original poster.