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

Author Topic: How to fix the error derived from the function "sf::Image::saveToFile()"?  (Read 1946 times)

0 Members and 1 Guest are viewing this topic.

iEPCBM

  • Newbie
  • *
  • Posts: 8
    • View Profile
Hey,
when I try to run program (its code below), I get an Application Error.

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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

iEPCBM

  • Newbie
  • *
  • Posts: 8
    • View Profile
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