SFML community forums

Help => Graphics => Topic started by: hypnotix on January 02, 2016, 10:48:35 pm

Title: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 02, 2016, 10:48:35 pm
I have this std::vector<std::unique_ptr<sf::Text>> and I cannot for the life of me understand why I am getting an "Access violation readin location" exception when pushing into it like so:

   returnValue.push_back( std::unique_ptr<sf::Text> ( new sf::Text(someString, someFont, 13) ) );

The font is loaded successfully and all, so its not that. Any insight as to why this is happening would be appreciated.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: dabbertorres on January 02, 2016, 11:42:37 pm
Can't tell what's going on without a minimal example.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 03, 2016, 10:09:29 am
I get: 'Exception thrown at 0x...(sfml-system-2.dll) in ...exe at 0x... Access violation reading location 0x...'
With the following code in debug mode in Visual Studio 2015 (compiled as release - win32, tested with the debugger)
#include <SFML/Graphics.hpp>
#include <vector>
#include <memory>

void main()
{    
        std::vector<std::unique_ptr<sf::Text>> vec;
        sf::Font test;
        test.loadFromFile("Arial.ttf");
        sf::Text text("hello ", test, 13); // does not throw
        vec.push_back(std::unique_ptr<sf::Text>(new sf::Text("goodbye ", test, 13))); // throws exception
}
 
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 03, 2016, 11:28:51 am
Same thing happens with
.emplace_back(new sf::Text("text", test, 13));
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 03, 2016, 11:56:30 am
Seems to happen at random as well, sometimes it doesn't crash, sometimes on constructing the first sf::Text, sometimes on the second one.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: Nexus on January 03, 2016, 05:58:00 pm
Make sure everything is linked correctly, and the SFML version matches your compiler. If necessary, rebuild SFML.

And emplacing dynamically allocated objects is a bad idea, you provoke memory leaks. Use std::make_unique() which is exception-safe.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 03, 2016, 06:16:27 pm
std::make_unique with sf::Text won't compile, it says sf::Text's move constructor is undefined.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 03, 2016, 06:24:43 pm
Crap, I was using SFML for VS 2013 in VS 2015, sorry.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 03, 2016, 06:33:08 pm
No dice, still get the exception. The address at which the access violation happens is 0xC0000005, if that helps.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: Laurent on January 03, 2016, 06:50:55 pm
Quote
No dice, still get the exception
Make sure that you make a full rebuild. And that wrong SFML binaries (in case of dynamic linking) are not still around.
Title: Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
Post by: hypnotix on January 03, 2016, 09:00:45 pm
Thanks, that did it.