SDL and std::vector not mixing well

So, I’m at a point where I need to use vectors to keep track of entities in my
game. I was a little rusty with vectors, so I just implemented them to keep
track of and record the average framerate. It all compiled fine, but the program
crashes on startup. After a little cross-reference to make sure I was using them
correctly (I was), it still don’t work. Simply declairing a vector crashes the
program.

  1. Is this a common problem?
  2. What causes this to happen?
  3. Is there any way around this?

So, I’m at a point where I need to use vectors to keep track of entities in my
game. I was a little rusty with vectors, so I just implemented them to keep
track of and record the average framerate. It all compiled fine, but the program
crashes on startup. After a little cross-reference to make sure I was using them
correctly (I was), it still don’t work. Simply declairing a vector crashes the
program.

I’m using std::vector (and other STL stuff) extensively combined with
SDL, and have never experienced any problems with it.
Maybe it would be easier to help if you could post a minimal piece of
code that still produces the problem?–
Rasmus Neckelmann

Rasmus Neckelmann <neckelmann gmail.com> writes:

I’m using std::vector (and other STL stuff) extensively combined with
SDL, and have never experienced any problems with it.
Maybe it would be easier to help if you could post a minimal piece of
code that still produces the problem?

vector vec;
^This crashes the program at startup.

vector vec(10);
^this won’t even compile… error C2059: syntax error : ‘constant’

I just tested it, that code works in other SDL apps,
but not the one I need it to.

Rasmus Neckelmann <neckelmann gmail.com> writes:

I’m using std::vector (and other STL stuff) extensively combined with
SDL, and have never experienced any problems with it.
Maybe it would be easier to help if you could post a minimal piece of
code that still produces the problem?

vector vec;
^This crashes the program at startup.

I’m guessing that your program has memory issues somewhere. Check for
double deletes, mixing new/delete malloc/free, buffer overruns, etc.

vector vec(10);
^this won’t even compile… error C2059: syntax error : ‘constant’

On what compiler? Cause that’s fine…
You might want to check the surrounding code. For example, forgetting
trailing semicolons after class declarations can produce weird errors on
some compilers (though usually that’s screens of random syntax errors).

I just tested it, that code works in other SDL apps,
but not the one I need it to.

As Rasmus asked, can you provide a minimal code example that fails?

Julian.On Monday 07 November 2005 11:02 pm, Nick Stovall wrote:


If it smells it’s chemistry, if it crawls it’s biology, if it doesn’t work
it’s physics.

Julian Peterson <weaver merold.net> writes:

Rasmus Neckelmann <neckelmann gmail.com> writes:

I’m using std::vector (and other STL stuff) extensively combined with
SDL, and have never experienced any problems with it.
Maybe it would be easier to help if you could post a minimal piece of
code that still produces the problem?

vector vec;
^This crashes the program at startup.

I’m guessing that your program has memory issues somewhere. Check for
double deletes, mixing new/delete malloc/free, buffer overruns, etc.

vector vec(10);
^this won’t even compile… error C2059: syntax error : ‘constant’

On what compiler? Cause that’s fine…
You might want to check the surrounding code. For example, forgetting
trailing semicolons after class declarations can produce weird errors on
some compilers (though usually that’s screens of random syntax errors).

I just tested it, that code works in other SDL apps,
but not the one I need it to.

As Rasmus asked, can you provide a minimal code example that fails?

Julian.

http://rafb.net/paste/results/amotZe10.html

Here you go, this is the entire header that I’ve declaired the vector in.

Replacing vector m_vFrameTracker with vector m_vFrameTracker( number
) doesn’t do any good, it still crashes.

I just tested it, and declairing a vector in main() works just fine, the program
runs nicely and every. Declairing it as a member of a class is what’s causing
the errors.

I’m using VS.NET2003> On Monday 07 November 2005 11:02 pm, Nick Stovall wrote:

Nick Stovall wrote:

http://rafb.net/paste/results/amotZe10.html
Here you go, this is the entire header that I’ve declaired the vector in.

Replacing vector m_vFrameTracker with
vector m_vFrameTracker( number) doesn’t do any good, it still
crashes.

I just tested it, and declairing a vector in main() works just fine, the
program runs nicely and every. Declairing it as a member of a class is
what’s
causing the errors.

My C++ might be a little rusty, but what exactly are you trying to do with
class CBaseEngine
{ …
vector m_vFrameTracker(1);
… }
?
That would be the syntax for constructing a vector with 1 element. You
cannot construct anything within the class declaration. Instead, it should
be declared as –
vector m_vFrameTracker;
and constructed like so –
CBaseEngine::CBaseEngine() : m_vFrameTracker(1) {}

-Alex

Alex Volkov <avcp-sdlmail usa.net> writes:

My C++ might be a little rusty, but what exactly are you trying to do with
class CBaseEngine
{ …
vector m_vFrameTracker(1);
… }
?
That would be the syntax for constructing a vector with 1 element. You
cannot construct anything within the class declaration. Instead, it should
be declared as –
vector m_vFrameTracker;
and constructed like so –
CBaseEngine::CBaseEngine() : m_vFrameTracker(1) {}

-Alex

I know that’s not proper syntax, I was just testing that and it’s what I
ended up pasting. I’ve tried constructing it like you said, it still
crashes on startup.

I got it all figured out. My box entity contains a pointer to an SDL_Rect and I
didn’t dynamically allocate it before I started using it… for some reason,
declairing a vector set that off and crashes the program. C++ memory management
is such a pain in the rear sometimes.