Jump to content
  • Advertisement
Sign in to follow this  
zauron

problems with std::map

This topic is 4829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My resource manager gets an exeption, but i cant see what could be wrong :(
#include "ResourceManager.h"
 
// textures is defined as std::map <std::string, IDirect3DTexture9 *> textures;
IDirect3DTexture9 *ResourceManager::getTexture(char *filename){
  IDirect3DTexture9 *tmp;
  std::string stringfilename = filename;
  // if it doesnt exist create it
  if( textures.find(stringfilename) == textures.end()){   //  <- get an exeption here, whats wrong with that?
  if( D3DXCreateTextureFromFile( p_d3ddevice, filename, &tmp ) != D3D_OK ){
    MessageBox(hWnd, filename, "Could not load texture", 0);
    return NULL;
  }
  textures[stringfilename] = tmp;
  // otherways return the one we have
  }else{
    return textures[stringfilename];
  }
  return tmp;
}

Share this post


Link to post
Share on other sites
Advertisement
I can't see why that would be generating an exception. Both the Standard Library reference docs I have and 'The C++ Standard Library' seem to make no mention of exceptions being thrown in either map::find or map::end, although in truth I only glanced briefly. What type of exception is getting thrown? If it is a standard exception type, what message is it's exception::what() method returning?

Share this post


Link to post
Share on other sites
Unhandled exception at 0x00409e5c in HeadOut.exe: 0xC0000005: Access violation reading location 0xbaadf2bd. is the error VC.net debug gives me.

It seams it only generates this if the file is a texture, so maybe the error actually refers to the line below... testing...

it actually works perfect as long as i dont call it from my map class.... well thanks for looking at it, will start looking through my other classes

[Edited by - zauron on May 29, 2005 9:03:21 AM]

Share this post


Link to post
Share on other sites
That's not a normal C++ exception - rather, it's the other kind (the "nasty" kind for lack of remembering the correct term :-) ). This says it all: 0xbaadf2bd

With many compilers, in debug mode "uninitialized" pointers and other "uninitialized" memory bits are initialized to 0xbaadf00d (bad food) or other fill patterns, to indicate that they're not really initialized. Are you sure you're not using your resource manager before it's been constructed? E.g.:

ResourceManager * manager;
manager->getTexture( "whatever" );


Something similar to this is probably the problem. I note ResourceManager because the line you're getting the exception on would appear to be the first line you access a member variable of ResourceManager (assuming textures isn't a global variable or something like that)

For more common fill patterns feel free to gander at this.

Share this post


Link to post
Share on other sites
in Engine class
ResourceManager rm;

so it is not a pointer

it is just when it gets called from the class Tile it crashes, so i am looking in there

Share this post


Link to post
Share on other sites
it was... in Tile i used engine->rm.getTexture
engine was pointing to 0xccccccc
thanks for the heads up on the memory, wold never hav spotted it otherwise

Share this post


Link to post
Share on other sites
Quote:
Original post by zauron
in Engine class
ResourceManager rm;

so it is not a pointer


There's a pointer somewhere. You're not doing this by any chance, are ya? Edit: Right here it looks like :D :

Engine * engine;
engine->rm.getTexture( "pie" );


Edit: Remainder of the post can be ignored, or read to hopefully glean some information about other possible causes for this problem so you have an easier time fixing the problem if it ever bites you in the ass again :-).

This will do the same thing - plus, this might explain the unusually large offset...: 0xBAADF2BD - 0xBAADF00D == 0x2B0 == 668 == how far off in bytes the pointer is from the "start".

Quote:
it is just when it gets called from the class Tile it crashes, so i am looking in there


There's two main things to look for:

1) Uninitialized pointers in your code. This can directly lead to this kind of thing.
2) Potentially not-yet-constructed variables such as contaniers. If the std::map isn't fully constructed yet, it may have some internal pointers that havn't been set to anything useful yet.

#2 can be a factor if you're tossing around global variables like so:

SomethingElse somethingElse;
Engine engine;

SomethingElse::SomethingElse( void )
{
//problem:
texture = engine.rm.getTexture( "Something Else's Texture" );
//we can't be sure that engine has been constructed before somethingElse!!!
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!