Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » global Object and static std::map --> Crash
icon5.gif  global Object and static std::map --> Crash [message #23208] Sat, 26 September 2009 01:38 Go to next message
loki is currently offline  loki
Messages: 36
Registered: October 2007
Member
Hello,

I have a class with a static std::map member. If I create an object of this class in a function, then I have no problems. But if I create a global Object of this class, then the program crashes when operating on the std::map member in the constructor...
If I compile with MSC9, then it crashes at startup. If compiled with GCC it works, but not correct. Data is missing.

I made a testcase to verify, that its not my fault in an other place. The Testcase i attached at the bottom. (Testcase sometimes runs. But most times not)

I found out, that the initializationorder of global objects is not defined. But does this also effect the static member?
I mean, that first the object is created and then its static member??? At the moment it looks for me like this.

Also I tried a construct like this, but does not work too.
map<string, int>& GetMap()
{
    static map<string, int> theMap = map<string, int>;
    return theMap;
}


Maybe I am blind, but I cant see a mistake.
Please help me with this.

main.cpp
#include "Texture.h"
#include <iostream>

pb::gl::Texture tex("ABC");

int main()
{
	std::cout << "In main" << std::endl;
	
	return 0;
}



Texture.h
#ifndef _Texture_Texture_h_
#define _Texture_Texture_h_

#include <map>
#include <string>
#include <iostream>

namespace pb {
namespace gl {


class Texture
{
	public:
				Texture(std::string id);
		
	private:
		static std::map<std::string, int>	textures;
};

}
}

#endif



Texture.cpp
#include "Texture.h"

using namespace pb::gl;
using namespace std;


map<string, int> Texture::textures = map<string, int>();

Texture::Texture(string id)
{
	map<string, int>::iterator element;
	element = textures.find(id); // <-- Crash inside of _tree
}


[Updated on: Sat, 26 September 2009 01:41]

Report message to a moderator

Re: global Object and static std::map --> Crash [message #23210 is a reply to message #23208] Sat, 26 September 2009 15:06 Go to previous message
loki is currently offline  loki
Messages: 36
Registered: October 2007
Member
After reading this: Link
I think, I just cant use my Class as a static one. The solution work, but then I cant load files in the constructor. I bet this is, caused by some other uninitialized data.
Previous Topic: template + convert problem
Next Topic: va_list problem
Goto Forum:
  


Current Time: Thu Jan 28 02:55:58 CET 2021

Total time taken to generate the page: 0.03311 seconds