SFML community forums

Help => General => Topic started by: hyperworker on February 26, 2018, 02:19:34 am

Title: Setting an sf::text vector element with elements from a string vector
Post by: hyperworker on February 26, 2018, 02:19:34 am
Hello all. I have a segmentation fault that I've been trying to narrow down for a while now. Now that I have the culprit, I am in dismay because I was sure that this was valid code. I have used vectors of sprites/vectors of textures to set and display objects before, but I did not think that this was needed with sf::Text, not in this context at least.

Here is a smaller version of the code I was using which reproduces the fault.


Assume that every line had a breakpoint, I have a marked in a comment where the segfault happens.


Quote
#include <iostream>
#include <vector>
#include <fstream>
#include <numeric>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <ostream>
#include <fstream>
#include <SFML/Window.hpp>



void hope(std::vector<std::string>& stringVector, std::vector<sf::Text> &theTextVector){

            int tempCounter=0;
    int textVectorCounter=theTextVector.size();
    textVectorCounter--;

    while(tempCounter<=textVectorCounter){

 theTextVector[tempCounter].setString(stringVector[tempCounter]);
 tempCounter++;}
        }  //Segmentation fault happens here.




}




Quote

int main()
{

 



sf::Text theText;

    std::vector<std::string> stringVector;

    stringVector.push_back("Hello.");
stringVector.push_back("How are you?");
stringVector.push_back("You're fine, thank you.");




std::vector<sf::Text> theTextVector(3);



hope(stringVector, theTextVector);



    std::cout<< stringVector[1];


    return 0;

}



While both vectors do end up within a new scope, they both come from outside of it and are both passed by reference. I thought that this was reason enough to make the code valid.

Thanks in advance for any replies.
Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: dmitry_t on February 27, 2018, 06:41:48 pm
So where does the segfault happen? There are no any comments in your code.
Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: hyperworker on February 27, 2018, 07:08:46 pm
Wow I am literally hallucinating. Checked several times before submitting the post and afterwards but it's not there.

Edited.
Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: eXpl0it3r on February 28, 2018, 12:38:11 am
Run it through your debugger and step through the loop while checking values.
Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: hyperworker on February 28, 2018, 07:15:59 am
That I have done. I've even removed the while loop and made each vector one element only just to see what would happen. Still the same thing.


It always points to this line in 'Atomicity.h '

{ return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }

Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: eXpl0it3r on February 28, 2018, 08:12:20 am
And a vector of std::string or any other type works?

What's the full callstack?
Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: hyperworker on February 28, 2018, 09:08:38 pm
Yes other vectors are working I have texture and sprite vectors in the main code that are working fine and the string vector itself does have the element I passed to it. I checked using cout. This string and text vector are the only vectors I am passing as a reference to a function though.

Here is the full call stack.



#0 0x401353   __gnu_cxx::__exchange_and_add(__mem=0xfffffffc, __val=-1) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/ext/atomicity.h:49)

#1 0x4013a3   __gnu_cxx::__exchange_and_add_dispatch(__mem=0xfffffffc, __val=-1) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/ext/atomicity.h:82)

#2 0x46189f   std::basic_string<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >::_Rep::_M_dispose(this=0xfffffff4, __a=...) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/bits/basic_string.h:246)

#3 0x4618f5   std::basic_string<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >::~basic_string(this=0x79fad4, __in_chrg=<optimized out>) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/bits/basic_string.h:547)

#4 0x4395cf   sf::String::~String(this=0x79fad4, __in_chrg=<optimized out>) (C:/Users/User/Documents/SFML/SFML-2.4.2-windows-gcc-4.9.2-tdm-32-bit/SFML-2.4.2/include/SFML/System/String.hpp:45)


#5 0x4391af   sf::Text::~Text(this=0x79fa28, __in_chrg=<optimized out>) (C:/Users/User/Documents/SFML/SFML-2.4.2-windows-gcc-4.9.2-tdm-32-bit/SFML-2.4.2/include/SFML/Graphics/Text.hpp:48)


#6 0x409c1b   main(argc=1, argv=0xe2f88) (C:\Users\User\Documents\SFML\main.cpp:2785)


Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: eXpl0it3r on February 28, 2018, 09:14:14 pm
Since it fails in std::string I'd assume that it's because your compiler doesn't match the version used to build SFML.
I suggest to build SFML from source with your compiler.
Also make sure you're not mixing debug and release mode.
Title: Re: Setting an sf::text vector element with elements from a string vector
Post by: hyperworker on February 28, 2018, 10:35:23 pm
This isn't the first sf::Text I've used in my program though, so I'm a little confused. I will go check on things just to be sure though.