Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Weird crash when drawing sprite  (Read 4103 times)

0 Members and 1 Guest are viewing this topic.

Abysmal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Weird crash when drawing sprite
« on: November 27, 2013, 06:56:37 pm »
Hi, I'm getting a really strange crash when trying to draw a sprite that is also a member of a class.
If I try this the program crashes with message "R6025 - pure virtual function call":
int MyClass::Run(void)
{
    Texture tex;
    if (!tex.loadFromFile("Image.png"))
        return EXIT_FAILURE;

    this->sprite = Sprite(tex);
    while (window.isOpen())
    {
        //Ignore the lack of event-handling
        window.clear();
        window.draw(this->sprite);
        window.display();
    }

    return EXIT_SUCCESS;
}
 
Commenting out this line "window.draw(sprite);" prevents it from crashing, but the window remains black as expected.

Additionally, if I simply replace this line "this->sprite = Sprite(tex);" with this line "Sprite sprite = Sprite(tex);" and update the draw call to match, the image is drawn successfully. It also works if I simply do this afterwards "Sprite sprite = this->sprite".
I really don't get why it crashes. It is the same sprite in all cases, the only difference is whether I use a local variable or a member variable in the draw call itself.
« Last Edit: November 27, 2013, 11:01:48 pm by Abysmal »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: Weird crash when drawing sprite
« Reply #1 on: November 27, 2013, 10:28:44 pm »
"pure virtual function call" is a common error and thus you'll get a lot of info about it on the internet. With the given code we can't pinpoint the issue. Can you please provide a complete but minimal example that reproduces the error? :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Abysmal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Weird crash when drawing sprite
« Reply #2 on: November 27, 2013, 11:00:26 pm »
Minimal example (crashes for me)
#include <SFML\Graphics.hpp>

using namespace sf;

class MyClass
{
public:
    Sprite sprite;

    int Run(RenderWindow &window);
};

int MyClass::Run(RenderWindow &window)
{
    Texture tex;
    if (!tex.loadFromFile("Image.png"))
        return EXIT_FAILURE;

    this->sprite = Sprite(tex);
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(this->sprite);
        window.display();
    }

    return EXIT_SUCCESS;
}

int main(void)
{
    RenderWindow window(VideoMode(600, 480), "test");

    MyClass test = MyClass();
    return test.Run(window);
}
« Last Edit: November 28, 2013, 03:06:23 am by Abysmal »

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Weird crash when drawing sprite
« Reply #3 on: November 27, 2013, 11:20:36 pm »
Code: [Select]
this->sprite = Sprite(tex);
This seems very strange to me. Though I'm not very good at SFML, I think it shoud look something like this:

Code: [Select]
sprite.setTexture(tex);
Probably you don't need to use "this" here. And

Code: [Select]
Sprite sprite;
should go to private section of class. Also, why do you do this?:

Code: [Select]
return test.Run();
Shouldn't this method be void, not int?

I assume you have a constructor, deconstructor...?
« Last Edit: November 27, 2013, 11:24:25 pm by mentor »

Abysmal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Weird crash when drawing sprite
« Reply #4 on: November 27, 2013, 11:36:25 pm »
This line
this->sprite = Sprite(tex);
is purely for creating a new Sprite instance with that texture. As for the other comments, the code above is purely a minimal example of crashing code and does not represent my actual code. In the actual code the member is private. Also, main() functions tend to return an exit code, i.e an int. At least in C.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Weird crash when drawing sprite
« Reply #5 on: November 27, 2013, 11:51:48 pm »
Quote
Code: [Select]
this->sprite = Sprite(tex);
This seems very strange to me. Though I'm not very good at SFML, I think it shoud look something like this:

Code: [Select]
sprite.setTexture(tex);
Probably you don't need to use "this" here.
It's valid and ok to do both of these things and always using this-> is a style some people choose, writing that is optional in c++ but there are languages that require it.

Quote
Code: [Select]
return test.Run();
Shouldn't this method be void, not int?
No, no at all. :'( http://www.stroustrup.com/bs_faq2.html#void-main standard main must return int, in both C and c++
Quote
I assume you have a constructor, deconstructor...?
It's called destructor and it's not needed here, compiler generates implicit ones that call constructors and destructors of all bases and members in right order and are enough in this(and most) cases.

Quote
Minimal example (crashes for me)
Doesn't even compile for me(aside from unportable \ in #include)..
hehe.cpp:40:21: error: no matching function for call to ‘MyClass::Run()’
     return test.Run();
                     ^
hehe.cpp:40:21: note: candidate is:
hehe.cpp:13:5: note: int MyClass::Run(sf::RenderWindow&)
 int MyClass::Run(RenderWindow &window)
     ^
hehe.cpp:13:5: note:   candidate expects 1 argument, 0 provided
 
int MyClass::Run(RenderWindow &window)
test.Run()
Does this seriously compile and run for you?
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: Weird crash when drawing sprite
« Reply #6 on: November 27, 2013, 11:57:42 pm »
As FRex stated it does not compile unless you add a RenderWindow, but then it runs flawless.

What compiler and OS are you using?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Weird crash when drawing sprite
« Reply #7 on: November 27, 2013, 11:59:46 pm »
Yeah, sorry, I meant destructor, not deconstructor :)

I meant that Run method should be void and later called in main like this:

Code: [Select]
test.Run();
And main ends with return 0 or return EXIT_SUCCESS... But if it's ok, then ok.

Abysmal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Weird crash when drawing sprite
« Reply #8 on: November 28, 2013, 02:57:25 am »
Wait wth, the code must've been switched with something else while I was working on the minimal version. Anyway, the OS is Windows 7 x64 Professional, and the code is compiled with Microsoft Visual Studio 2012 Ultimate. The program crashes with this message "R6025 - pure virtual function call" and three buttons: "Break", "Retry" and "Ignore". When I press "Break" the debugger gives some obscure file which differs from time to time.

Anyway, I updated the minimal version above with what I have open in Visual Studio right now.

Edit:

Interesting update. This morning I tested it again, and it didn't crash, but later on the day it came back again. So the last hour I've been wondering what could cause this. The only constant is that I also happen to have the PSX emulator running in the background when it crashes. Could be some memory issues with either one.
« Last Edit: November 28, 2013, 10:10:17 am by Abysmal »

 

anything