Hello.
This code across 4 files:
Base.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
#ifdef DEFINE_MACRO
#define MY_MACRO true
#endif
class Base
{
public:
#if MY_MACRO
bool b = true;
#endif
sf::Shader shader;
void func();
};
Base.cpp
#define DEFINE_MACRO
#include "Base.h"
void Base::func()
{
shader.loadFromFile("shader.frag", sf::Shader::Fragment);
shader.setUniform("color", sf::Glsl::Vec4(1.f, 0.5f, 1.f, 1.f));
cout << MY_MACRO << endl;
}
shader.frag
#version 120
uniform vec4 color;
void main()
{
gl_FragColor = color;
}
main.cpp
#include "Base.h"
int main()
{
Base base;
base.func();
}
compiles, but upon running it in default visual studio debug configuration this gets printed out:
An internal OpenGL call failed in Shader.cpp(849).
Expression:
GLEXT_glDeleteObject(castToGlHandle(m_shaderProgram))
Error description:
GL_INVALID_VALUE
A numeric argument is out of range.
("1" from cout is not printed and program never exits by itself).
In release configuration the program throws:
Unhandled exception at 0x7A66BC3F (sfml-graphics-2.dll) in main.exe: 0xC0000005: Access violation reading location 0x00000004.If lines "#if MY_MACRO" and "#endif" are removed, the program runs as expected, printing out "1".
May be worth mentioning that this minimal example doesn't produce the exact errors the bigger SFML project does, them being: "
Exception thrown: read access violation. _Pnext was 0xDDDDDDE1." in debug and either: "
Exception thrown at 0x7997AFE0 (sfml-graphics-2.dll) in main.exe: 0xC0000005: Access violation reading location 0x0114818D." or: "
Unhandled exception at 0x777B6D13 (ntdll.dll) in main.exe: 0xC0000374: A heap has been corrupted (parameters: 0x777F3960)." in release configuration. There were few (but very rare) instances when the program ran normally despite not changing anything in the code. So, by just restarting the program over and over, I keep getting different errors, or very rarely no errors. Removing "#if MY_MACRO" and "#endif" lines fixes everything just like in the example. All of this is tested with SFML 2.5.1 though. I'm not sure what causes this as nothing else breaks when using macros like this.
While compromising fix of not using macros is fine, I wonder what is the problem here? What causes this? Is it possible to fix? Is this way of using macros incorrect or unintended by SFML?
Thank you for your time