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

Author Topic: Access violation when pushing into vector<std::unique_ptr<sf::Text>>  (Read 5188 times)

0 Members and 3 Guests are viewing this topic.

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
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.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #1 on: January 02, 2016, 11:42:37 pm »
Can't tell what's going on without a minimal example.

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #2 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
}
 

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #3 on: January 03, 2016, 11:28:51 am »
Same thing happens with
.emplace_back(new sf::Text("text", test, 13));

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #4 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #5 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #6 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.

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #7 on: January 03, 2016, 06:24:43 pm »
Crap, I was using SFML for VS 2013 in VS 2015, sorry.
« Last Edit: January 03, 2016, 06:27:23 pm by hypnotix »

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #8 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.
« Last Edit: January 03, 2016, 06:34:44 pm by hypnotix »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #9 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.
Laurent Gomila - SFML developer

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Access violation when pushing into vector<std::unique_ptr<sf::Text>>
« Reply #10 on: January 03, 2016, 09:00:45 pm »
Thanks, that did it.

 

anything