Page 1 of 1

[Solved] SDL crashing at blit

Posted: Sat Mar 12, 2011 11:41 am
by ParticleGames
Basically, I have a function that blits for me and it is crashing when I try to blit a bullet I create.

So first, I have an abstract base class named "Ship" that contains this:

Code: Select all

std::vector<Bullet*> bullets;
Then I have a derived class named "Player" that tries to add to its vector a MedBullet, which derives from bullet:

Code: Select all

void Player::shoot()
{
    if(wantShoot == true)
    {
        if(bulletCount.is_started()==false || bulletCount.get_ticks()>200)
        {
            MedBullet bullet(x+(images[EplayerShip]->w/2)-5,y-7);
            bullets.push_back(&bullet);

            if(bulletCount.is_started() == false)
            {
                bulletCount.start();
            }else{
                bulletCount.stop();
                bulletCount.start();
            }
        }
    }
}
Then when I try to do the events for the bullets:

Code: Select all

for(int i = 0; i < (int)bullets.size(); i++)
    {
        bullets[i]->show(); //It crashes here
Show is an abstract base function in Bullets, but in MedBullet it is:

Code: Select all

void show()
{
        apply_surface(x,y,images[EmedBullet],screen,NULL);
}

//apply surface
void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface * destination, SDL_Rect* clips)
{
    SDL_Rect offset;
    offset.x=x;
    offset.y=y;

    if(source != NULL)
    {
        SDL_BlitSurface(source, clips, destination, &offset);
    }
}
So at bullets->show(); my game terminates with status 3. Any help?

Re: SDL crashing at blit

Posted: Sat Mar 12, 2011 12:03 pm
by dandymcgee
ParticleGames wrote:So at bullets->show(); my game terminates with status 3. Any help?

Seriously dude, stop using Code::Blocks and go download Visual Studio 2010 Express. You will never have to deal with such ridiculously useless errors again.

To fix your problem:

Code: Select all

for(int i = 0; i < (int)bullets.size(); i++)
{
	if(bullets[i] != NULL)
		bullets[i]->show(); //It crashes here
}
"status 3" usually means your trying to call a method against a null object. Visual Studio would've told you that in english. ;)

In general, it's a generic error that simple means "Uh-oh, something broke." because Code::Blocks isn't intelligent enough to tell you what broke.

Re: SDL crashing at blit

Posted: Sat Mar 12, 2011 1:52 pm
by Ginto8
I'm pretty sure it should have said that it received SIGSEGV if you ran it in Debug, but I haven't used C::B in forever :|

Re: SDL crashing at blit

Posted: Sat Mar 12, 2011 2:52 pm
by adikid89
My guess.. those MedBullets are temp.. got destroyed and the pointer is thus useless..

Re: SDL crashing at blit

Posted: Sat Mar 12, 2011 7:32 pm
by ParticleGames
I made the switch to VS 2010 Express. Adding the NULL tester isn't working either, it doesn't detect as NULL.
In my debugger, a window pops up after the crash and it says:

Unhandled exception at 0x01192bc3 in Shootforlife.exe: 0xC0000005: Access violation reading location 0xcccccccc.

My output from debug is:

First-chance exception at 0x01192bc3 in Shootforlife.exe: 0xC0000005: Access violation reading location 0xcccccccc.
Unhandled exception at 0x01192bc3 in Shootforlife.exe: 0xC0000005: Access violation reading location 0xcccccccc.

I also updated my first post with my bullet creation. I also figured out in my debugger that my bullets values are huge negative numbers. Any help now?

Re: SDL crashing at blit

Posted: Sat Mar 12, 2011 7:45 pm
by dandymcgee
From http://en.wikipedia.org/wiki/Magic_number_(programming)
Wikipedia wrote:CCCCCCCC Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory

Re: SDL crashing at blit

Posted: Sat Mar 12, 2011 8:16 pm
by ParticleGames
Any idea why it is uninitialized?

EDIT: Fixed! The variables I were putting into my vector were created in a function, therefore only being temporary. So all I had to do is chance the parameters of the push_back() function to a dynamically allocated MedBullet. Thanks guys! VS seems like a better IDE, too.

Re: SDL crashing at blit

Posted: Sun Mar 13, 2011 11:25 am
by adikid89
ParticleGames wrote:Any idea why it is uninitialized?

EDIT: Fixed! The variables I were putting into my vector were created in a function, therefore only being temporary. So all I had to do is chance the parameters of the push_back() function to a dynamically allocated MedBullet. Thanks guys! VS seems like a better IDE, too.
adikid89 wrote:My guess.. those MedBullets are temp.. got destroyed and the pointer is thus useless..
I win one internetz!
Just remember.. if (you new them) { ... delete them! }
else { memory leaks! }

Re: SDL crashing at blit

Posted: Sun Mar 13, 2011 12:00 pm
by thejahooli
adikid89 wrote:
ParticleGames wrote:Just remember.. if (you new them) { ... delete them! }
else { memory leaks! }
Your code is flawed. This would mean that whenever you don't use 'new' you are causing a memory leak. The correct way of doing this would be similar to the following:

Code: Select all

if(you new them)
{
	if(!delete them)
	{
		memory leaks!
	}
}

Re: [Solved] SDL crashing at blit

Posted: Sun Mar 13, 2011 12:26 pm
by ParticleGames
Yes I implemented deletes along with it.