std::map causes segfaults

Started by
7 comments, last by Seoushi 18 years, 4 months ago
I can't understand what is going wrong with my program. I can use use map in a test program but in my engine I'm writting I can't get it map to perform any functions without getting a segfault. I declare the map like this:
std::map< uint, sTexture > m_Textures;
sTexture is a structure for my textures and here is its declaration:
struct sTexture
{
	GLuint Texture;
	std::string Filename;
	GLfloat Width;
	GLfloat Height;
};
Then when I try to access the map or do anything with it it segfaults. I've tried a few functions of map, here are the ones make it crash.
m_Textures.empty();
m_Textures.size();
m_Textures[ place ] = Tex;
For reference place is a uint and Tex is a sTexture struct. Here is the exact error message:
Fatal signal: Segmentation Fault (SDL Parachute Deployed)
Any idea why map would cause a segfault?
Advertisement
Sounds like a buffer overflow is trashing your std::map's contents.

Are you able to use std::map in another, simple application without it segfaulting?
My guess is that your code is causing the segmentation fault, not std::map.
yes I am able to use it in a simple program. At the moment that is called there is nothing in map. The only thing that the engine has done on the poin of getting to that code is loaded a surface from a file via sdl_image and then converting that to an opengl texture. And I do remeber to free the sdl surface after it has been converted.
Well I tried a few things.. I made a map in the main loop (outside of the class it is in) with the same structure and it worked so something is going wrong with it inside the class. I commented out all the code in my class and tried a clear and it still doesn't seem to want to work.. the code I'm using isn't too terribley big so I'm gonna post what I have (without the commented out part).

texture manager header
#ifndef TEXTMAN#define TEXTMAN#include "sTexture.h"#include "../globals.h"#include "SDL/SDL_Image.h"//for windows#ifndef GL_BGR	#define GL_BGR GL_BGR_EXT#endif#ifndef GL_BGRA	#define GL_BGRA GL_BGRA_EXT#endifclass cTextureManager{	public:			cTextureManager();			~cTextureManager();			uint LoadTextureFromFile(std::string filename);			uint LoadTextureFromMemory(SDL_Surface* Surface, std::string filename = "");			uint CreateTextureSection(uint TextureId, sSection Section);			void RemoveTexture(uint TextureId);            void RemoveTextureSection(uint SectionId);            void DrawTextureSection(uint SectionId, GLfloat X, GLfloat Y, GLfloat Scale = 1, GLfloat Rotatation = 0);            void DrawTexture(uint SectionId, GLfloat X, GLfloat Y, GLfloat Scale = 1, GLfloat Rotatation = 0);            //sTexture* getTexture(uint TextureId);			//sTextureSection* getTextureSection(uint SectionId);			void RemoveAllTextures();			void RemoveAllTextureSections();	protected:            std::map< uint, sTexture > m_Textures;            std::map< uint, sTextureSection > m_TextureSections;};#endif


offending functions
uint cTextureManager::LoadTextureFromFile(std::string filename){    SDL_Surface* surface;    return LoadTextureFromMemory(surface, filename);}uint cTextureManager::LoadTextureFromMemory(SDL_Surface* Surface, std::string filename){    m_Textures.clear();}


When my main gameloop runs it call LoadTextureFromFile("valid filename");
then it gets to the textures.clear line and segfaults.

keep in mind that this code doesn't actually do anything since I commented it, the commented code runs fine without the map stuff in there as well. I'm preety sure I'm doing something wrong but I don't know what. For reference I have posted my whole program on my site http://www.seoushi.com/misc/Kore.zip if you have codeblocks, sdl image, sdl and opengl installed it compiles fine.

thanks for the help so far.


Edit:
here is the backtrace to my error
#0 0x0043fd32 std::_Rb_tree<unsigned int, std::pair<unsigned int const, sTexture>, std::_Select1st<std::pair<unsigned int const, sTexture> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, sTexture> > >::_M_begin (??:??)
#1 0x0043fb46 std::_Rb_tree<unsigned int, std::pair<unsigned int const, sTexture>, std::_Select1st<std::pair<unsigned int const, sTexture> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, sTexture> > >::clear (??:??)
#2 0x0043e1dd std::map<unsigned int, sTexture, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, sTexture> > >::clear (??:??)
#3 0x004036c9 cTextureManager::LoadTextureFromMemory (??:??)
#4 0x00403649 cTextureManager::LoadTextureFromFile (??:??)
#5 0x004047a1 Example::Run (??:??)
#6 0x00404ab4 SDL_main (??:??)
#7 0x00405c01 console_main (win32/SDL_win32_main.c:249)
#8 0x00405db1 WinMain (win32/SDL_win32_main.c:361)
#9 0x004056da main (??:??)

[Edited by - Seoushi on November 29, 2005 3:54:18 PM]
Please use [ source] and [ /source] around the code to makes our lives so much easier.

#ifndef TEXTMAN#define TEXTMAN#include "sTexture.h"#include "../globals.h"#include "SDL/SDL_Image.h"//for windows#ifndef GL_BGR	#define GL_BGR GL_BGR_EXT#endif#ifndef GL_BGRA	#define GL_BGRA GL_BGRA_EXT#endifclass cTextureManager{	public:			cTextureManager();			~cTextureManager();			uint LoadTextureFromFile(std::string filename);			uint LoadTextureFromMemory(SDL_Surface* Surface, std::string filename = "");			uint CreateTextureSection(uint TextureId, sSection Section);	                void RemoveTexture(uint TextureId);                        void RemoveTextureSection(uint SectionId);                        void DrawTextureSection(uint SectionId, GLfloat X, GLfloat Y, GLfloat Scale = 1, GLfloat Rotatation = 0);                        void DrawTexture(uint SectionId, GLfloat X, GLfloat Y, GLfloat Scale = 1, GLfloat Rotatation = 0);                        //sTexture* getTexture(uint TextureId);			//sTextureSection* getTextureSection(uint SectionId);			void RemoveAllTextures();			void RemoveAllTextureSections();	protected:            std::map< uint, sTexture > m_Textures;            std::map< uint, sTextureSection > m_TextureSections;};#endif


uint cTextureManager::LoadTextureFromFile(std::string filename){    SDL_Surface* surface;    return LoadTextureFromMemory(surface, filename);}uint cTextureManager::LoadTextureFromMemory(SDL_Surface* Surface, std::string filename){    m_Textures.clear();}


EDIT: Lined up 3 functions I somehow messed up.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Quote:Original post by Seoushi
uint LoadTextureFromMemory(SDL_Surface* Surface, std::string filename = "");uint cTextureManager::LoadTextureFromFile(std::string filename){    SDL_Surface* surface;    return LoadTextureFromMemory(surface, filename);}



While I haven't used SDL, I'm going to go out on a limb here and bet that this is where the problem is. LoadTextureFromMemory appears to want a valid Surface from its declaration, although it would've been more clear had it taken a reference. However, it is taking a pointer, which signifies that Surface can either be NULL, or valid. You are passing an invalid pointer, which satisfies neither.
--Michael Fawcett
The problem is that your texture manager doesn't exist. You never allocated it.

You declare a number of uninitialised global pointers. You then use one of those uninitialised global pointers to try and initialise all of the global pointer by calling a member function (undefined behaviour even if it will tend to work). That member function in turn declares new local variables with the same names as the global variables and initialises them. Execution then leaves the member function destroying the local variables and leaking the memory associated with them.

Then you try and make a call to a member function of your texture manager, which still hasn't been initialised. This tries to use a std::map member variable, but the variable isn't a map, it's just random uninitialised memory pretending to be a map. Since the random uninitialised memory doesn't meet the preconditions of std::map your program crashes.

Enigma
I planned to intialize the everything in my graphics class but I didn't initalize that. Doh. Thanks for your help. I knew I was doing something small and stupid, it always is :).

This topic is closed to new replies.

Advertisement