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

Author Topic: Empty image semantics  (Read 1381 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Empty image semantics
« on: December 26, 2010, 03:04:33 pm »
Although this is rather a theoretical problem, I want to inform you about some observations I've made. The following code fails with a STL debug assertion (VS 2010):
Code: [Select]
#include <SFML/Graphics/Image.hpp>
int main()
{
sf::Image image;
image.SaveToFile("image.bmp");
}

The problem is the statement &pixels[0] in ImageLoader.cpp. As far as I know, if you access the 0th element of an empty std::vector, you evoke undefined behaviour.

There are a few other places (also in Image.cpp) where the access is unchecked. In some of them, you could handle it like in Image::CreateMaskFromColor():
Code: [Select]
if (myPixels.empty())
        return;

But I don't know if there is a convention how to represent empty image files. Anyway, you should probably make the semantics clearer or explicitly forbid some operations on singular sf::Image instances.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Empty image semantics
« Reply #1 on: December 26, 2010, 04:04:58 pm »
This is absolutely true. It's a shame that I left so many undefined behaviours in sf::Image :P

I reviewed all the functions and made sure that the behaviour is always well-defined and robust.

Thanks :)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Empty image semantics
« Reply #2 on: December 27, 2010, 05:11:03 pm »
Thank you :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: