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

Author Topic: Single line of code breaks program.  (Read 2491 times)

0 Members and 1 Guest are viewing this topic.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Single line of code breaks program.
« on: October 02, 2019, 09:57:03 pm »
I am having a proble m with a personal project of mine. Whenever I try to remove this line in file "Canvas.cpp" in function draw my program breaks. Nothing is displayed.

File: Canvas.cpp Function: draw() Line: 69
if(!circle.isSelectingSize())
{
    texture.draw(circle); //this line
}
 

Github page for code :https://github.com/Xrey274/Splash/tree/beta

I know it's a big project with many files, but I have removed every trace of the Circle class, even from CMakeLists.txt(meaning it was not even compiled), but no matter what I did this problem persisted. If you are on Linux you can compile it for youself and try it out.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Single line of code breaks program.
« Reply #1 on: October 03, 2019, 12:18:09 am »
This sounds like undefined behavior.

It's highly likely that the crash is not related to this code, especially since removing it triggers it. Instead, you may cause UB somewhere else in your program, which just happens to "work" in some cases, but there is in fact a bug.

UB is very hard to track down, because it may or may not manifest itself, close or far from the code that causes it. As such, you should try to isolate the problem and review existing code.

Things that are inherently unsafe and you should avoid at all costs in modern C++:
  • Raw arrays -> use std::vector and std::array, which have range checks in debug mode
  • Manual memory management with new/delete, or raw owning pointers -> use RAII
  • Self-made data structures -> use STL or other libraries
  • Low-level pointer arithmetics, byte-level copies and casts. Abstract them and use higher-level operations such as move/copy constructors.
  • Global variables, singletons etc. with very few exceptions (and never use it for SFML classes) -> design your program in a way that it doesn't need those
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Single line of code breaks program.
« Reply #2 on: October 03, 2019, 06:32:29 am »
Problem is that I am not doing any of those or nearly. Also the program is not crashing, the gui(from TGUI library) stops appearing along with the canvas(which is really just a class that inherits from sf::Sprite with a renderTexture inside it). It's almost as if the clear() command is repeadetly called. When I try to draw on the canvas things appear for a fraction of a section then disappear.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Single line of code breaks program.
« Reply #3 on: October 03, 2019, 01:32:30 pm »
I found out that Circle::isSelectingSize is causing a segmenationFault(for somereason) so I removed it and found out that the whole canvas is corrupted the world over. It not only static, but also random images I don't even have on my pc.

https://imgur.com/a/6ms95Ee

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Single line of code breaks program.
« Reply #4 on: October 03, 2019, 02:10:39 pm »
You aren't adjusting the texture rect when you set the render texture texture to the canvas sprite.
Not sure if this is really the issue, but in theory you could have a larger texture rect thus revealing some uninitialized GPU texture buffer data.

Personally, I'd recommend to derive the canvas from sf::Drawable instead, implement the derived draw function and composite a sprite.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Single line of code breaks program.
« Reply #5 on: October 03, 2019, 03:49:23 pm »
May I ask what is the texture rect?

I don't think this is the case, as at line 11 in Resources.cpp I clearly define the size of the Canvas.

    canvas.setSize(sf::Vector2f(1408, 792));

Just found out that when I remove line 12 at Resources.cpp the artifacts are gone. That line applied the context settings(antialiasing 8). Now I have to find out why the Canvas doesn't show up at all.
« Last Edit: October 03, 2019, 04:04:33 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Single line of code breaks program.
« Reply #7 on: October 03, 2019, 04:29:25 pm »
Yeah thanks for the links. I have figured out that the problem this whole time was applying antiAliasing to the renderTexture. Any ideas why that would happen?

    textureSettings.antialiasingLevel = texture.getMaximumAntialiasingLevel();

    texture.create(size.x, size.y, textureSettings);

 

anything