PDA

View Full Version : Vector problems


gardon
12-02-2006, 10:55 AM
Quick memory question regarding textures.

I'm coding a memory/image manager in C++ and SDL, and am running into trouble. AFter debugging I"ve figured out that my class is working properly, but it has soemthing to do with the vectors I"m using.

Currently, I have a vector of sTexture structs, that are declared as follows:

struct sTexture
{
SDL_Surface*;
int width;
int height;
}

Where SDL_Surface* is used to store image data.

and in my initialization:


sTexture newTexture;

newTexture.width = w;
newTexture.height = h;
newTexture.surface = textureSurface;

m_Textures.push_back(newTexture);

The following code above was addressed in a function where the width, height, and surface was passed.


My problem is when I'm trying to draw it, I use the surface from the texture I've pushed back onto the vector using m_Textures[whateverNumber].surface

The memory instance is not there. What's wrong? I explicitly call the appropriate functions. It's as if a vector can not store pointer data.

Jason

.oisyn
12-02-2006, 12:13 PM
I don't know what the problem is, but I can tell you it has nothing to do with the vector itself.

SigKILL
12-02-2006, 02:48 PM
The problem could be that textureSurface has been deleted (may possibly be a local variable somewhere), or that there a no sufficient = operator (or possibly copy constructor) for sTexture (try to create one explicitly if you haven't done so). If you're a destructor abuser you might delete textureSurface in the sTexture destructor (which would cause problems). I could probably continue to give 1000 other possibilities...

gardon
12-02-2006, 05:32 PM
No it's not that either, I've checked. I purposely made sTexture a struct (thus the s prefix) so I wouldn't run into this problem.

I dont' know, I"m going nuts.

Reedbeta
12-02-2006, 06:08 PM
You said you used a debugger; can't you successively narrow down the section of code in which the bug occurs until you're close enough to see it? Bearing in mind that it's possible part of your code outside this class is stomping on the memory somehow.

gardon
12-02-2006, 06:23 PM
Yes. It narrows down to where I shuve the sTexture struct into the vector

gardon
12-02-2006, 06:50 PM
I found the direct reason that's causing it.

I've eliminated all other elements besides this one: loading memory through functions.

The only way it works is if I load the surface into memory just before the draw (in the draw function).

The moment I take that: m_surface = SDL_LoadBMP

and put it into another function, even if I call that function right before the draw function, everything goes nuts. Is it my computer perhaps? It seems as if it can't keep track of memory.

Reedbeta
12-02-2006, 10:36 PM
How about if you zip up your code and upload it someplace; someone here can download it and see if they encounter the same behavior.

juhnu
12-03-2006, 12:53 AM
Might it be you are freeing your SLD (texture) surface twice?

.oisyn
12-03-2006, 05:23 AM
Yeah it sounds like heap corruption. Either you're deleting a pointer multiple times like juhnu suggests, you're deleting a pointer you've never allocated, or you're writing past your buffers.

Usually when this happens, you will never notice it at the place and time where it goes wrong, but rather later on when allocating or freeing a pointer in a piece of code that has nothing to do with your actual bug.

Goz
12-05-2006, 01:25 AM
Yuor initialisation code block REALLY looks like you are creating a local variable. Once you leave that function that data will no longer exist and the struct will get overwritten ...

.oisyn
12-05-2006, 02:19 AM
Nonsense, the vector stores a copy of that variable, which makes perfect sense.

Goz
12-05-2006, 03:18 AM
Nonsense, the vector stores a copy of that variable, which makes perfect sense.

Ahh yeah ... damn mornings. If it had been

m_Textures.push_back(&newTexture);

I might be talking sense ;)