Sign in to follow this  
Damocles

std::map - using char* for indexes is crashing the app

Recommended Posts

So I'm making a manager class that will allow me store and retrieve pointers by name, and so far it looks like this: Header:
struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};


class WindowManager  
{
private:
	map <const char*, Window*, ltstr> table;
public:
	WindowManager();
	virtual ~WindowManager();
	void AddWindow(Window* win, const char* name);
	Window* GetWindow(const char* name);
};
Body:
#include "WindowManager.h"

WindowManager::WindowManager()
{
}
WindowManager::~WindowManager()
{
}

void WindowManager::AddWindow(Window* win, const char* name)
{
	table[name]=win;   // crashes here
}

Window* WindowManager::GetWindow(const char* name)
{
	return table[name];
}
and it always crashes when trying to set an indexed value. All my googling seems to suggest that the code should work fine, but it always crashes. Anyone know where the problem lies?

Share this post


Link to post
Share on other sites
it's important to remember that you are storing pointers, you are NOT storing strings. My guess is that the app is crashing because the "string" who's pointer you store, goes out of scope, so the table is now pointing to invalid memory.

-me

Share this post


Link to post
Share on other sites
yeah that's exactly why it's crashing. you've previously stored a string that's gone out of scope. when you use your functor to compare table keys, you are acessing invalid memory.

-me

Share this post


Link to post
Share on other sites
Use std::string. Always. It will save you so many random crashes its not even funny.

As far as I can tell its a drop in replacement, this code compiles for me:


class Window; // I have no window class :(
class WindowManager
{
private:
std::map <std::string, Window*> table;
public:
WindowManager();
virtual ~WindowManager();
void AddWindow(Window* win, const char* name);
Window* GetWindow(const char* name);
};


WindowManager::WindowManager()
{
}
WindowManager::~WindowManager()
{
}

void WindowManager::AddWindow(Window* win, const char* name)
{
table[name]=win;
}

Window* WindowManager::GetWindow(const char* name)
{
return table[name];
}



Share this post


Link to post
Share on other sites
I don't think it's a scope issue. I use this...

AddWindow(winPtr, "some name");

...to add the data. It crashes on the very first attempt to add data to the map array. It hasn't had a chance to go out of scope, and I always thought that any strings that you write directly into the code as "some string" will remain in scope always because they are compiled into the exe.

rip-off:

I tried switching over to std::string, coded exactly as you suggested, and it compiles fine here too, but crashes in the exact same place as before. :(

Share this post


Link to post
Share on other sites
Are you sure your WindowManager object is valid? That is, there's no things like:
WindowManager* mgr;
mgr->AddWindow(win, "Foo"); // BOOM! Just garbage in mgr

Quote:
Original post by Damocles
It hasn't had a chance to go out of scope, and I always thought that any strings that you write directly into the code as "some string" will remain in scope always because they are compiled into the exe.


This is correct. However, when you or somebody else someday accidentally do something like this:

void foo()
{
char foo[] = "Foo";
mgr.AddWindow(win, foo);
}
There will be pain.

Share this post


Link to post
Share on other sites
ok, I'm an idiot (and my excuse is that it's late here and very very hot and humid).

It was indeed my forgetting to init my WindowsManager object.

Damn, I haven't made a cock-up like that for years. Especially with failing to realize till someone else pointned it out. Must be time for bed I think.

Thanks Sharlin.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this