SFML community forums

Help => General => Topic started by: ggggggggggggggg on April 16, 2013, 04:38:02 am

Title: Weird error when dealing with Clock and Time
Post by: ggggggggggggggg on April 16, 2013, 04:38:02 am
When the value of shoot speed is assigned to 0 - 3, the program exits without an error. However, with a value more than 3, this lovely error comes up - "Unhandled exception at 0x770015de in Practice  SFML.exe: 0xC0000005: Access violation reading location 0xfeeefef6."

Here's the two functions that use these variables.

Turent::Init-
void Turent::Init()
{
        sprite.setPosition(400, 300);
        sprite.setTexture(textureManager.turent);
        sprite.setOrigin(sprite.getGlobalBounds().width / 2, sprite.getGlobalBounds().height / 2);
        InScreen.setSize(sf::Vector2f(1024, 768));
        shootSpeed = sf::milliseconds(100); //line where I change the value
}

Turent::Shoot-
void Turent::Shoot(sf::RenderWindow& Window)
{
        if(shootClock.getElapsedTime().asSeconds() > shootSpeed.asSeconds() && sf::Mouse::isButtonPressed(sf::Mouse::Left)) //line where it's used
        {
                Bullet blt;
                blt.Setup(sprite.getPosition().x, sprite.getPosition().y, rotation + 90, sf::Mouse::getPosition(Window).x, sf::Mouse::getPosition(Window).y);
                bullets.push_back(blt);
                shootClock.restart();
        }
}
 

Note these were working perfectly until it stopped.
Title: Re: Weird error when dealing with Clock and Time
Post by: Laurent on April 16, 2013, 07:45:06 am
You're accessing freed memory. That's all I can say, so use your debugger to find out more.
Title: Re: Weird error when dealing with Clock and Time
Post by: ggggggggggggggg on April 16, 2013, 09:53:47 pm
I don't see how. In my header file I have
sf::Clock shootClock;
and in my .cpp file I have
if(shootClock.getElapsedTime().asSeconds() > 1 && sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
                Bullet bullet;
                bullet.Setup(sprite.getPosition().x, sprite.getPosition().y, rotation + 90, sf::Mouse::getPosition(Window).x, sf::Mouse::getPosition(Window).y);
                bullets.push_back(bullet);
                shootClock.restart();
        }

It only gives an error with this
shootClock.getElapsedTime().asSeconds() > 1

When I comment it out the turent shoots super fast but it shuts down right.
Title: Re: Weird error when dealing with Clock and Time
Post by: Laurent on April 16, 2013, 10:08:39 pm
Quote
I don't see how
That's why debuggers exist...
Title: Re: Weird error when dealing with Clock and Time
Post by: ggggggggggggggg on April 16, 2013, 10:17:11 pm
Right. Ok, thanks.
Title: Re: Weird error when dealing with Clock and Time
Post by: ggggggggggggggg on April 16, 2013, 10:25:24 pm
So I found out why.. I guess. I don't see how this would be even related, but I guess it was.

In my main I had an FPS counter.. For some reason it only gave an error when another clock in another file was used.. Is that normal? They didn't touch each other at all.
Title: Re: Weird error when dealing with Clock and Time
Post by: Laurent on April 17, 2013, 08:00:05 am
Seriously, what's wrong with the debugger? It will show you exactly where the application crashes. Don't waste your time like this.