Jump to content


Vector problems


12 replies to this topic

#1 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 02 December 2006 - 06:55 PM

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

#2 .oisyn

    DevMaster Staff

  • Moderators
  • 1822 posts

Posted 02 December 2006 - 08:13 PM

I don't know what the problem is, but I can tell you it has nothing to do with the vector itself.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#3 SigKILL

    Valued Member

  • Members
  • PipPipPip
  • 200 posts

Posted 02 December 2006 - 10: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...

#4 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 03 December 2006 - 01:32 AM

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.

#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 03 December 2006 - 02:08 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#6 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 03 December 2006 - 02:23 AM

Yes. It narrows down to where I shuve the sTexture struct into the vector

#7 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 03 December 2006 - 02:50 AM

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.

#8 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 03 December 2006 - 06:36 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#9 juhnu

    Valued Member

  • Members
  • PipPipPip
  • 292 posts

Posted 03 December 2006 - 08:53 AM

Might it be you are freeing your SLD (texture) surface twice?

#10 .oisyn

    DevMaster Staff

  • Moderators
  • 1822 posts

Posted 03 December 2006 - 01:23 PM

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.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#11 Goz

    Senior Member

  • Members
  • PipPipPipPip
  • 574 posts

Posted 05 December 2006 - 09: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 ...

#12 .oisyn

    DevMaster Staff

  • Moderators
  • 1822 posts

Posted 05 December 2006 - 10:19 AM

Nonsense, the vector stores a copy of that variable, which makes perfect sense.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#13 Goz

    Senior Member

  • Members
  • PipPipPipPip
  • 574 posts

Posted 05 December 2006 - 11:18 AM

.oisyn said:

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 ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users