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

Author Topic: window.draw() unhandled exception when passing in a sf::Text  (Read 2358 times)

0 Members and 1 Guest are viewing this topic.

cadeProj

  • Newbie
  • *
  • Posts: 2
    • View Profile
Firstly I have put this problem here since I do not know if it is my use of window.draw() or my sf::text creation/passing around that is the problem.

I have a sfml project that is complex and large, it ran perfectly fine, I have added a new feature and cannot now run the functionality the feature provides. I am not going to put all the code up initially as I assume the issue lies within the addition (which is complex enough) as the program ran fine before. Any class member variables used in the code below was properly initiated and ran fine before the additions were made.

Initial essential details, the main program runs there is a screen manager that opens a title window, in the screen manager's draw function there is a pointer pointing to which ever screen is currently the new screen's draw function. Here are the relevant code sections then I will explain the issue.


 sf::String DoubleSFStringConverter(double temp)
{
        std::stringstream ssDuration;  std::cout << "1" << endl ;
        ssDuration << temp ;std::cout << "1" << endl ;
        return ssDuration.str().c_str() ; std::cout << "1" << endl ;
       
}

sf::Text SubResult(sf::String tempString,
                                   sf::Font tempFont,
                                   double tempData,
                                   double tempResultNo,
                                   double tempColumn)
{
        sf::Text ResultBondType ;std::cout << "1" << endl ;
        ResultBondType.setString(tempString + DoubleSFStringConverter(tempData)) ; std::cout << "1" << endl ;
        ResultBondType.setPosition((331 * tempColumn),(75 + (16*tempResultNo))) ; std::cout << "1" << endl ;
        ResultBondType.setScale(0.5,0.5) ; std::cout << "1" << endl ;
        ResultBondType.setFont(tempFont) ; std::cout << "1" << endl ;
        ResultBondType.setColor(sf::Color::Black) ; std::cout << "1" << endl ;
        std::cout << "1" << endl ;
        return ResultBondType ;

}

sf::Text SubResultStr(sf::String tempString,
                                   sf::Font tempFont,
                                   double tempResultNo)
{
        sf::Text ResultBondType ;std::cout << "1 \n" ;
        ResultBondType.setString(tempString) ; std::cout << "2 \n" ;
        ResultBondType.setPosition(0,(75 + (16*tempResultNo))) ; std::cout << "3 \n" ;
        ResultBondType.setScale(0.5,0.5) ; std::cout << "4 \n" ;
        ResultBondType.setFont(tempFont) ; std::cout << "5 \n" ;
        ResultBondType.setColor(sf::Color::Black) ; std::cout << "6 \n" ;

        return ResultBondType ;
}

 

Three functions, the first converts a member variable (temp) into a string and returns it, the second and third create and return a text, note the first function being fed in setString. std::couts are there to keep track of program flow. NOTE: in all instances all these functions run through and return fine. I am hesitate about adding 2 Strings together but this is addressed later.   


vector <sf::Text> DiscreteRateBond::PrintResults(sf::RenderWindow &aWindow)
{
        sf::Font TextFont ;
       
        std::cout << "1" << endl ;
        if (!TextFont.loadFromFile("SerifFont.ttf"))
    std::cout << "Could not find specific font" ;
        std::cout << "2" << endl ;

        //Also could pass const references to improve efficiency instead of by value for subResult...()
        vector<sf::Text> ResultsTable ; std::cout << "a" << endl ;
        ResultsTable.push_back(SubResultStr("Bond with Discrete Interest Rate Payments",TextFont,0));std::cout << "3" << endl ;
        ResultsTable.push_back(SubResultStr(BondName, TextFont, 1) );std::cout << "4" << endl ;
        ResultsTable.push_back(SubResult("Duration: ",TextFont, BondDuration, 0, 1) ); std::cout << "5" << endl ;
        ResultsTable.push_back(SubResult("Maucualay Duration: ", TextFont, BondMacaulayDuration, 1, 1)) ; std::cout << "6" << endl ;
        ResultsTable.push_back(SubResult("Convexity: ", TextFont, BondConvexity, 2, 1)) ; std::cout << "7" << endl ;
        ResultsTable.push_back(SubResult("Yield to Maturity: ", TextFont, BondYieldToMaturity, 3,1)) ; std::cout << "8" << endl ;
        ResultsTable.push_back(SubResult("Modified Duration: ", TextFont, BondModifiedDuration, 4 , 1)) ; std::cout << "a9" << endl ;
       
        std::cout << "a" << endl ;
       
        Results = ResultsTable ; std::cout << "10a" << endl ;
       
        return ResultsTable ;
       
}
 


This member function (of DiscreteRateBond) creates a vector<st::Text> ResultsTable, adds the return of one of the 2 functions above (subresult or subresultstr) as an element, sets the public member variable Results to the array before also returning the array (I know might be better to have void but not the issue). Note again std::cout used to check program flow and the font being used in other screens I wouldn't think that is the problems (loads fine).



void BookClassResults::Draw(sf::RenderWindow &Window)
{
//addition start
        vector <double> cflow ; cflow.push_back(10) ; cflow.push_back(10) ; cflow.push_back(110) ;
        vector <double> Times ; Times.push_back(1) ; Times.push_back(2) ; Times.push_back(3) ;
        DiscreteRateBond DiscreteBondExample("Example 1", cflow,Times, 0.8) ;
        DiscreteBondExample.PrintResults(Window) ;
//addition end
       
        Window.draw(Background) ;

        BCRNavigationBar.Draw(Window) ;
        BCRInfoBar.Draw(Window);
        BCRresultsBar.Draw(Window) ;
        std::cout << "BCRNAVINFO\n" ;
        Window.draw(text) ;

//addition start
        vector<int>::iterator iter;

        for (std::vector<int>::size_type i = 0; i != DiscreteBondExample.Results.size(); i++)
        {
                std::cout << i << endl ;
                Window.draw(DiscreteBondExample.Results[i]) ; //problem seems to be here
                std::cout << "fffff" << endl ;
        }

//addition end
       
}

 

BookClassResults is the current screen and the draw function functioned fine before the additions (surrounded by addition comments). The class DiscreteRateBond is simple, has worked and constructs fine setting all member variables fine in previous examples (even this example is copy n pasted from another screen). Now this is where the problem arises. In the program flow everything runs fine and the last two print to screens are
"BCRNAVINFO"
"0"
 

Which means that all the functions have run through fine, set everything they need to set fine and the first i is printed ("0") and the exception throws when the first Window.draw(DiscreteBondExample.Results i) is called. Note DiscreteBondExample.Results i should be an element of the DiscreteBondExample.Results vector<sf::Text>, the element being a text object so should draw fine. Final note the three code blocks are in 3 seperate cpp files with relevant header files included and inclusion guarded. So that isn't a problem.

The next item on the call stack is: >   sfml-graphics-d-2.dll!std::_Tree<std::_Tmap_traits<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page> >,0> >::_Lbound(const unsigned int & _Keyval) Line 2095   C++


Which in the code file: xtree, is :


                while (!this->_Isnil(_Pnode))
                        if (_DEBUG_LT_PRED(this->_Getcomp(), this->_Key(_Pnode), _Keyval))
                                _Pnode = this->_Right(_Pnode);  // descend right subtree
                        else
                                {       // _Pnode not less than _Keyval, remember it
                                _Wherenode = _Pnode;
                                _Pnode = this->_Left(_Pnode);   // descend left subtree
                                }

 


SOOOOO anyone? Can anyone help? I think it might have to do with the text properties itself as the results array was a later addition to attempt to fix it. The addition of 2 strings in setstring in the first code block might have worried me if the first element to be fed into window.draw() did not have this and still the exception is thrown at the first window.draw(discretebond.results i) .

My initial thought was if Text loses properties when it is return or something that could cause an issue of trying to draw an empty, no property, text. As the returns of functions are what are being drawn that could be it but core concept wise I don't understand how an object could lose properties when it is being locally copied and returned. My first attempt just had a list of window.draws at the end of the second code block and the function called in the third so I do not think it has anything to do with the STL stuff.

Edit: Further detail : First-chance exception at 0x567FB52C (sfml-graphics-d-2.dll) in sml2.0.exe: 0xC0000005: Access violation reading location 0x0100DBC9.

Unhandled exception at 0x567FB52C (sfml-graphics-d-2.dll) in sml2.0.exe: 0xC0000005: Access violation reading location 0x0100DBC9.

The thread 0x1af4 has exited with code 0 (0x0).
« Last Edit: July 28, 2013, 01:32:16 am by cadeProj »

cadeProj

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: window.draw() unhandled exception when passing in a sf::Text
« Reply #1 on: July 28, 2013, 10:34:29 am »
Right I have semi fixed it, seems like you cannot set the Text's properties outside the draw function, here's the fix, BUT I would still like to know why this happened. I couldn't see anything wrong with setting the Text's properties then returning the entire text from the function and drawing it.


void BookClassResults::Draw(sf::RenderWindow &Window)
{
//addition start
    vector <double> cflow ; cflow.push_back(10) ; cflow.push_back(10) ; cflow.push_back(110) ;
    vector <double> Times ; Times.push_back(1) ; Times.push_back(2) ; Times.push_back(3) ;
    DiscreteRateBond DiscreteBondExample("Example 1", cflow,Times, 0.8) ;
    DiscreteBondExample.PrintResults(Window) ;
//addition end
   
    Window.draw(Background) ;

    BCRNavigationBar.Draw(Window) ;
    BCRInfoBar.Draw(Window);
    BCRresultsBar.Draw(Window) ;
    std::cout << "BCRNAVINFO\n" ;
    Window.draw(text) ;

//addition start
    vector<int>::iterator iter;

    for (std::vector<int>::size_type i = 0; i != DiscreteBondExample.Results.size(); i++)
    {
        std::cout << i << endl ;

//Fix

DiscreteBondExample.Results[i].setFont(font) ; // font is the screens font
DiscreteBondExample.Results[i].setColor(Color::Black) ;
DiscreteBondExample.Results[i].setScale(0.5,0.5) ;

//end of fix

        Window.draw(DiscreteBondExample.Results[i]) ; //problem seems to be here
        std::cout << "fffff" << endl ;
    }

//addition end
   
}


 


 

anything