0

I'm trying to learn C++. I'm trying to get into GUI-programming with the frame work SFML (based on OpenGL) and TGUI. When I run my basic program (just window initialisation and creating a button) in Visual Studio (debug mode), it throws the exception: Access violation reading location 0xCCCCCCCC. I found out, that 0xCCCCCCCC is a specific memory pattern and means I am doing stuff with uninitialised memory on the stack. But where am I doing this in my program? My Code:

#include <TGUI/Gui.hpp>
#include <TGUI\TGUI.hpp>
#include <TGUI\Widgets\Button.hpp>

using namespace std;

int main() {
    tgui::Button::Ptr button = tgui::Button::create("Test");   //HERE THE EXCEPTION IS THROWN
    sf::RenderWindow window(sf::VideoMode(800, 600), "Fenster");
    tgui::Gui gui(window);
    gui.add(button, "Hallo");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            // When the window is closed, the application ends
            if (event.type == sf::Event::Closed)
            {
                window.close();
            } else if (event.type == sf::Event::Resized){
                window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
                gui.setView(window.getView());
            }

            // Pass the event to all the widgets
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
    }

delete button;
button = NULL; 
return EXIT_SUCCESS;
}

Any help would be nice. Thanks!

Ben
  • 201
  • 1
  • 10
  • Possible duplicate of [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new) – user0042 Nov 25 '17 at 18:43
  • Have you tried creating the `RenderWindow` before the button? Maybe TGUI needs SFML to have already been initialized (by creating a `RenderWindow` first). Or perhaps also create the `tgui::Gui` instance first too. – Nikos C. Nov 25 '17 at 18:48
  • I don't know anything about TGUI, but it doesn't need any initialization first? Perhaps you should take a look [at their minimal example](https://tgui.eu/tutorials/v0.7/minimal-code/)? – Some programmer dude Nov 25 '17 at 18:49
  • @user0042: I think it isn't, because i know, what the value 0xCCCCCCCC means, but I can't see the situation the value will be assigned in my code. – Ben Nov 25 '17 at 18:49
  • I'll try it, thanks. – Ben Nov 25 '17 at 18:51
  • I tried it, but the exception still appears. – Ben Nov 26 '17 at 08:07
  • Are you certain the TGUI and SFML libraries are compatible? Are they for your exact compiler version and did you download both SFML 2.4.2 and TGUI from their respective download pages? Are you using the debug or release versions of both? Crashes in TGUI like this on windows can happen if you don't use the exact same SFML library than was used to build TGUI with. I'm also a bit surprised that "delete button;" even compiles (it doesn't on gcc), button is a shared_ptr and you should never try to delete its pointer. – texus Nov 26 '17 at 09:51
  • I added "delete button" when I asked the question, I don't have it in my code, oops :-) (I have to study pointers more). – Ben Nov 26 '17 at 10:02
  • The TGUI version is exactly for my SFML version... – Ben Nov 26 '17 at 10:03
  • I am using Visual Studio 2017, do you think this could be a problem? – Ben Nov 26 '17 at 10:09
  • @Ben Neither SFML nor TGUI provides official libraries for VS2017, so if you didn't compile SFML or TGUI yourself then it may indeed be a problem (although VS2017 is supposed to be compatible with VS2015). I have uploaded VS2017 compatible SFML and TGUI libraries at https://www.dropbox.com/s/f48henthn67kajs/TGUI%200.7.6%20-%20SFML%202.4.2%20-%20VS2017%20-%2032bit.zip?dl=0 (32bit) or https://www.dropbox.com/s/ithmb8tdddrtc0q/TGUI%200.7.6%20-%20SFML%202.4.2%20-%20VS2017%20-%2064bit.zip?dl=0 (64bit), try using these libraries AND headers instead of the ones you were using. – texus Nov 26 '17 at 10:33
  • Thanks, I will try it. – Ben Nov 26 '17 at 14:18
  • If i run it, it says x.dll is missing (x is equal to tgui.dll etc.). This is normal, and I pasted those files in the debug folder. But there is still a dll missing: sfml-system-2.dll. But I didn't find sfml-system-2.dll in the tgui and sfml folder you posted. – Ben Nov 27 '17 at 16:23
  • @Ben It seems like my build system doens't keep the dlls (it only keeps what is needed to build the TGUI libs). So with the files I sent you have to link statically. Btw, you should mention me when you comment, because I don't get a notification for it otherwise. – texus Nov 27 '17 at 17:16
  • @texus What do I have to do for static linking? – Ben Nov 28 '17 at 14:30
  • @texus Ok, I have to use the -s DLLs, set the used runtime framework to multithreaded, and what else? – Ben Nov 28 '17 at 14:33
  • @Ben Don't change the runtime framework. Just use the -s libs, define SFML_STATIC (add it to Preprocessor Definitions) and also link to the SFML dependencies according to https://www.sfml-dev.org/tutorials/2.4/start-vc.php. The libraries you link to should thus look like this (in Release mode): "winmm.lib opengl32.lib winmm.lib gdi32.lib opengl32.lib freetype.lib jpeg.lib sfml-system-s.lib sfml-window-s.lib tgui-s.lib". In debug mode, the sfml and tgui libraries need "-d" at the end as well. – texus Nov 28 '17 at 17:44
  • @texus But when I try it, Visual Studio says: fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86' . How can I fix this? – Ben Nov 29 '17 at 14:28
  • @texus Ok, i just fixed this, i set it wrong. But now, it says (very often), that some values don't match (LNK2038). – Ben Nov 29 '17 at 14:32
  • @Ben Which values? Make sure to link to release libraries in release mode and debug libraries (with -d postfix) in debug mode and make sure runtime framework is set to its default value (I think it is /MD and /MDd). – texus Nov 29 '17 at 17:11
  • @texus For example, _ITERATOR_DEBUG_LEVEL doesn't match, and so on. – Ben Nov 30 '17 at 14:21
  • @texus Ok, but when i remove the SFML_STATIC, those errors disappear, and now there are new ones: VS says, I have a bunch of links to unresolved external symbols (LNK2019), for example : __declspec(dllimport) public: __thiscall sf::String::String(char const *,class std::locale const &)" (__imp_??0String@sf@@QAE@PBDABVlocale@std@@@Z)" in Funktion "_main" – Ben Nov 30 '17 at 14:23
  • IT WORKS! (After including other libs, i didn't read your comments good enough, I thought, winmm.lib, opengl.lib, and so on; wouldn't really be necessary for my code; thanks for your help! and sorry, I really didn't realized it) – Ben Nov 30 '17 at 14:36

0 Answers0