1
votes

I'm using Visual Studio 2017. When debugging it points me to line 11

texture.getTexture().copyToImage().saveToFile("C:/test.bmp");

with an error

Exception thrown at 0x54B06B9E (sfml-graphics-2.dll) in Project2.exe: 0xC0000005: Access violation reading location 0xCCE6C37F.

I want to save the texture to a .bmp file.

#include<iostream>
#include<SFML/Graphics.hpp>

int main()
{
    using namespace sf;
    RenderTexture texture;
    texture.create(800, 600);
    texture.display();
    texture.clear(Color::Black);
    texture.getTexture().copyToImage().saveToFile("C:/test.bmp");

    return 0;
}

EDIT

As far as we've detected it's the .saveToFile("C:/test.bmp") fragment that's causing the issue, the code works fine without it

SECOND EDIT

I got SFML packages manually, I include them from a set folder each time I'm making a new project and link the libraries, also added manually through external lib folder

THIRD EDIT

After some work I managed to fix the debug libraries and release libraries, now the code throws an exception

Run-Time Check Failure #2 - Stack around the variable 'texture' was corrupted.

A screenshot with the whole output and code: enter image description here

Another thing is, when I continue without handling the exception it throws at me this:

Unhandled exception at 0x00D26859 in Project2.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

2
You could make things easier by breaking up that last line to see exactly which one of those calls is causing the exception. - PaulMcKenzie
@PaulMcKenzie it's the .saveToFile function as far as I can see, it runs just fine without .saveToFile - Yee7i
Did you open VS with Administrator rights? Had you try to save it in another folder, less restrictive than C:? Maybe My Documents or similar could work. - alseether
@alseether opening as administrator didn't work, changing location to "D:\output\test.bmp" neither - Yee7i
The docs note that "This function fails if the image is empty.". I would think that means that the function returns false, but maybe it means that it invokes undefined behavior? Try moving the texture.clear line before texture.display. - 0x5453

2 Answers

1
votes

My best guess is that you're linking the libraries somehow wrong. Ensure you have debug libraries in debug configuration and release ones in release configuration.

Remember, debug libraries usually end with a "d". If you don't set debug libraries correctly, you cannot debug your code and that would explain why you're getting a poor error message.

I leave here a link to SFML forum with someone with a similar issue.

Hope that helps.


As side note, for beginners with I highly recommend install the library using NuGet packages (right click on your solution -> Manage NuGet packages -> look for SFML -> install)

This works for SFML, not sure for OpenGL.

0
votes

I found an answer, the issue was that I wasn't using .dlls compiled for VS 2017, I was using those compiled for 2015 instead, switched them and it works like a charm