Jump to content
  • Advertisement
Sign in to follow this  
boejunda

Strings have shattered my once-cool program.

This topic is 4022 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

I've been working on a game engine using NeHe's OpenGL tutorials. I few days ago, I had completed a working prototype that loaded a bunch of graphics and displayed them as texture masked quads, and it did it very well! I even felt comfortable turning it in for some independent study credits that I needed. But my point is, the thing WORKED. So what changed? I decided to implement std::string instead of using character arrays, and now my program is screwed. I've been working at it for days now, but the state of my engine is only going downhill, and I need the wisdom of Gamedev before I decide to just kill the whole project (which would be an extremely rash move, considering the amount of work I put into it thus far). Here's what is happening now... I have a Graphic class that combines a string ID and a reference to a loaded texture. The class is also responsible for loading said textures, which is when the ID and reference number are assigned. It was when I changed the ID from a character array to a string that memset started crashing my program. Here's the code: <code> bool Graphic::load (string fileName) { /* Temporary storage for the file being loaded from. */ AUX_RGBImageRec * image; memset(image, 0, sizeof(void *) * 1); /* Try loading the bitmap from file. If it fails, the operation will return false. */ bool status = false; if (image = loadBMP(fileName)) { status = true; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } /* Free the original bitmap. */ if (image) { if (image->data) free(image->data); free(image); } return status; } </code> This piece of code worked flawlessly. When I changed ID into a string, memset started causing an Unhandled Exception fault, which did not happen at all until then. So what gives? Why is is happening now? I understand that std::string is safer than character arrays, but this is not the only problem I've had since implementing strings. This Graphic class gets inserted into a std::list, and that no longer works either... it only inserted garbage data, but that was until I started having THIS problem. I'm at my wits end.

Share this post


Link to post
Share on other sites
Advertisement
why would you memset a pointer??


AUX_RGBImageRec * image = NULL;



What your program is doing is trying to memset the memory pointed at by the uninitialized pointer image. the first argument is a pointer of the memory to be set, not the variable to be set.

the actual way to use memset in that case would be:

AUX_RGBImageRec * image;

memset(&image, 0, sizeof(void *) * 1);



note the extra '&' before image.

But never ever ever initialize a variable that way. it's a waste.

-me

Share this post


Link to post
Share on other sites
memset(image, 0, sizeof(void *) * 1);

image points to invalid memory. You're writing 0 to some random memory address. What are you trying to do here???

Share this post


Link to post
Share on other sites
Oh god... I must have removed the & before image when I was screwing around. It is
somewhat operational again.
Sorry guys, looks like it was a simple typo that went unnoticed all day. SORRY.

Share this post


Link to post
Share on other sites
Yeah, I guess I read the wrong FAQ. Now I am enlightened.

So here's my previous problem. I'm trying to create a list of Graphic
References. Here's the code that creates a new Graphic, loads it and
then inserts it into the list. The problem is, it only inserts garbage.

I think it has something to do with the copy constructor or the = operator
I added, but I can't figure out what.



bool Databank::loadGraphic (string id, string fileName) {

/* Create a new Graphic object, and if it loads the requested image
properly, place it in the Graphic list. Otherwise, return false.
*/


Graphic newGraphic(id);

if (newGraphic.load(fileName)) {

graphics.push_back(newGraphic);
return true;

}

return false;

}




And the class defintiion...



class Graphic {

private:

/* The ID for this Graphic.
*/


string id;


/* A reference to a texture (The common name for an image that is used in
3D applications).
*/


GLuint texture;


public:

/* Constructors, copy Constructor, Deconstructor.
*/


Graphic ();
Graphic (string idIn) {id = idIn;}
Graphic (const Graphic &rhs);
~Graphic ();


/* Overloaded operators which are required for templated lists of classes.
*/


Graphic &operator= (const Graphic &rhs);
int operator== (const Graphic &rhs) const;
bool operator!= (const Graphic &rhs) const;
bool operator> (const Graphic &rhs) const;
int operator< (const Graphic &rhs) const;



/* Load a graphic file into memory.
*/



bool load (string fileName);


/* Accessors.
*/


string getID () {return id;}
GLuint getTexture () {return texture;}

};



And the relevant constructors and whatnot.


Graphic::Graphic () {

id = "";
texture = NULL;

}





Graphic::Graphic (const Graphic &rhs) {

id = rhs.id;
texture = rhs.texture;

}





Graphic::~Graphic () {

}


Graphic& Graphic::operator= (const Graphic &rhs) {

return *this;

}




Am I just missing something crucial here? I know that std::list makes a copy
of whatever data is added to it, and that's the need for the above elements.
But I can't figure out why this isn't working.

Share this post


Link to post
Share on other sites
Do you really want to copy that much data each time you insert a Graphic object into the list? What you should be doing is storing a list of pointers, so that when you call push_back() you're only copying the size of the pointer and not the entire contents of Graphic.

Modify loadGraphic() so that it allocates Graphic objects on the heap and keep a std::list<Graphic*> or...

Take a look at boost::ptr_list.

Share this post


Link to post
Share on other sites
Quote:
Original post by thecolonel
Do you really want to copy that much data each time you insert a Graphic object into the list? What you should be doing is storing a list of pointers, so that when you call push_back() you're only copying the size of the pointer and not the entire contents of Graphic.

Modify loadGraphic() so that it allocates Graphic objects on the heap and keep a std::list<Graphic*> or...

Take a look at boost::ptr_list.


All he's copying is a string and an int.

In any case, all that I can see is your assignment operator doesn't actually assign.

Share this post


Link to post
Share on other sites
Quote:
Original post by boejunda
Oh god... I must have removed the & before image when I was screwing around. It is
somewhat operational again.
Sorry guys, looks like it was a simple typo that went unnoticed all day. SORRY.


Once the dust has settled on your project enough to give you an hour or so to spare, look in to setting up a version control system. My favourite is Mercurial, but there are lots of others to investigate.

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!