[Solved] SDL crashing at blit

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

[Solved] SDL crashing at blit

Post 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?
Last edited by ParticleGames on Sat Mar 12, 2011 9:40 pm, edited 2 times in total.
Check out my iPhone app here: http://twilighthop.com
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SDL crashing at blit

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL crashing at blit

Post 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 :|
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: SDL crashing at blit

Post by adikid89 »

My guess.. those MedBullets are temp.. got destroyed and the pointer is thus useless..
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Re: SDL crashing at blit

Post 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?
Check out my iPhone app here: http://twilighthop.com
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SDL crashing at blit

Post 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
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Re: SDL crashing at blit

Post 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.
Check out my iPhone app here: http://twilighthop.com
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: SDL crashing at blit

Post 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! }
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: SDL crashing at blit

Post 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!
	}
}
I'll make your software hardware.
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Re: [Solved] SDL crashing at blit

Post by ParticleGames »

Yes I implemented deletes along with it.
Check out my iPhone app here: http://twilighthop.com
Post Reply