SFML community forums

Help => Graphics => Topic started by: iEPCBM on August 02, 2016, 12:55:19 pm

Title: How to fix the error derived from the function "sf::Image::saveToFile()"?
Post by: iEPCBM on August 02, 2016, 12:55:19 pm
Hey,
when I try to run program (its code below), I get an Application Error.
(http://rishatkagirov.tk/MISC_PHOTOS/ERROR01.bmp)
Here is the English translation of error text:
The instruction at "0x7c910fda" addressed at "0xffff0048". The memory could not be "written".

Here is the code of the program:
main.cpp:
#include <SFML/Graphics.hpp>

int main()
{
    sf::Color pixelColor;

    sf::Image image;
    if (!image.loadFromFile("pic.jpg"))
        return EXIT_FAILURE;

    for (int i=0;i<=image.getSize().x;i++) {
        for (int j=0;j<=image.getSize().y;j++) {
            pixelColor=image.getPixel(i,j);
            image.setPixel(i,j,sf::Color(pixelColor.r,pixelColor.g,255));
        }
    }

    if (!image.saveToFile("pic2.jpg"))//Here is the error
        return EXIT_FAILURE;

    return EXIT_SUCCESS;
}
 
With the help of debug I figured out that this error occurs here:
if (!image.saveToFile("pic2.jpg"))//Here is the error
        return EXIT_FAILURE;
 
How to fix it?
Title: AW: How to fix the error derived from the function "sf::Image::saveToFile()"?
Post by: eXpl0it3r on August 02, 2016, 01:04:20 pm
Are you sure the error occurs where you say it does?
Your loop iterations are wrong. The image size goes from 0 to size - 1, yet you iterate from 0 to size (< vs <=), as such setPixel will get out of bounds and you get undefined behavior.
Title: Re: How to fix the error derived from the function "sf::Image::saveToFile()"?
Post by: iEPCBM on August 02, 2016, 04:51:09 pm
Are you sure the error occurs where you say it does?
Your loop iterations are wrong. The image size goes from 0 to size - 1, yet you iterate from 0 to size (< vs <=), as such setPixel will get out of bounds and you get undefined behavior.
Oh Yes! You are absolutely right! Sorry for my carelessness.  :D