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

Started by
6 comments, last by Palidine 17 years, 10 months ago
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?
------------------------------------------[New Delta Games] | [Sliders]
Advertisement
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
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
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];}


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. :(
------------------------------------------[New Delta Games] | [Sliders]
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.
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.
------------------------------------------[New Delta Games] | [Sliders]
sounds like your WindowManager object is garbage. what's the value of the this pointer when it breaks in that function?

-me

This topic is closed to new replies.

Advertisement