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

Author Topic: setFont from a font loaded from memory crashes program  (Read 3340 times)

0 Members and 1 Guest are viewing this topic.

ccleung6

  • Newbie
  • *
  • Posts: 22
    • View Profile
setFont from a font loaded from memory crashes program
« on: October 02, 2012, 04:31:21 am »
Hi all,

I loaded a font file from memory, then immediately setFont to a sf::Text, but don't know why the program crashes and force to quit.

bool ok = m_font.loadFromMemory(byteArr.data(),byteArr.size()); // byteArr is QByteArray stored in a QMap
if(ok)
{
        qDebug() << "load font from memory ok";
        m_text.setFont(m_font); // call this function crashes program
}else
{
        qDebug() << "load font from memory failed";
}
 

but I use sf::Font::loadFromFile() is OK, draw text correctly

QString path = "....a font file path...";
m_font.loadFromFile(path.toStdString());
m_text.setFont(m_font);
 

where m_font is a sf::Font member variable of a RenderWindow subclass
m_text is sf::Text member variable of the same RenderWindow subclass
the font file used is a Chinese font with size > 5MB

PS: I use Qt 4.7.4 with MinGW

Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: setFont from a font loaded from memory crashes program
« Reply #1 on: October 02, 2012, 08:09:42 am »
It works for me.

#include <SFML/Graphics.hpp>
#include <fstream>
#include <vector>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML text");

    std::vector<char> buffer;
    std::ifstream file("C:/Windows/Fonts/arial.ttf", std::ios_base::binary);
    file.seekg(0, std::ios_base::end);
    std::streamsize size = file.tellg();
    file.seekg(0, std::ios_base::beg);
    buffer.resize(size);
    file.read(&buffer[0], size);

    sf::Font font;
    if (!font.loadFromMemory(&buffer[0], buffer.size()))
        return -1;

    sf::Text text;
    text.setString("hello world");
    text.setFont(font);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(text);
        window.display();
    }

    return 0;
}

So can you please post a complete and minimal code that reproduces the problem?
Laurent Gomila - SFML developer

ccleung6

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: setFont from a font loaded from memory crashes program
« Reply #2 on: October 02, 2012, 09:20:41 am »
Thanks for your reply, Laurent.

I find out the reason.

I should pass a const char* pointer instead just of char* to loadFromMemory(const void *data, std::size_t sizeInBytes)

this works fine
m_font.loadFromMemory(byteArr.constData(),byteArr.size());
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: setFont from a font loaded from memory crashes program
« Reply #3 on: October 02, 2012, 09:37:47 am »
Hmm... that's really strange.
Laurent Gomila - SFML developer

Qix

  • Full Member
  • ***
  • Posts: 139
  • I am Qix!
    • View Profile
    • Natoga Technologies
Re: setFont from a font loaded from memory crashes program
« Reply #4 on: October 02, 2012, 02:35:09 pm »
Probably a really stupid question, but what happens when you typecast to a (const char*)?
~ Qix
Creator of Rippl Studio
Code: [Select]
<danharibo> iostream: I don't do enough drugs to think that's a good idea.