SFML community forums

Help => General => Topic started by: rabbie on January 19, 2017, 06:04:11 am

Title: 2.4.1 Memory leak with sf::Text?
Post by: rabbie on January 19, 2017, 06:04:11 am
Hello, just recently I've noticed an odd memory leak with sf::Text in one of my project. That being because of how I'm on [Windows 7 with Visual Studio 2015 Community and thus using the CRT library to detect memory leaks.

Anyways, I've been able to recreate the memory leak report by using the member function setOutlineThickness on a text object and then proceeding to draw that object to a window. When I don't give the text an outline thickness I get no leak report. Also, If I specify the outline thickness and don't draw I don't get the report. More so, the type of font the text is using doesn't matter, I get the same results.

I've also commented parts of my code to give insight and also to show the lines that are suspected to be causing this leak. Can anyone else see if I'm doing something wrong or is possibly able to replicate this?

#define _CRTDBG_MAP_ALLOC
#include <SFML\Graphics.hpp>
#include <iostream>
#include <stdlib.h>
#include <crtdbg.h>

void test() {
        sf::RenderWindow window(sf::VideoMode(720, 360), "TESTING");

        sf::Font font;
        font.loadFromFile("media/fonts/default/arial.ttf"); //font has no effect, same results.
        sf::Text text("This is a pretty freaking cool font.", font, 24U);
        text.setOutlineColor(sf::Color::White);
        text.setOutlineThickness(1.75f); //enabling this results in a memory leak only if the resulting text is drawn, otherwise no leak.
        text.setFillColor(sf::Color::Green);

        while (window.isOpen()) {
                sf::Event ev;
                while (window.pollEvent(ev)) {
                        if (ev.type == sf::Event::Closed) {
                                window.close();
                        }
                }
                text.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)));

                window.draw(text); // if the text has a value for outlineThickness there is a memory leak when it's drawn, otherwise there is no leak.
                window.display();
                window.clear(sf::Color::Black);
        }
}

void textTest() {
        sf::Font font;
        font.loadFromFile("media/fonts/default/arial.ttf");

        sf::Text txt("Test", font, 24U);
        txt.setOutlineColor(sf::Color::Black);
        txt.setOutlineThickness(1.5f);
        txt.setFillColor(sf::Color::White);
        txt.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition()));
}

int main() {
        _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); //automatically dump a report on exit
       
        test();

        //textTest(); //No memory leak when it is constructed even when you specify outlineThickness.
        return EXIT_SUCCESS;
}
 

Thank you for your time and help.
Title: Re: 2.4.1 Memory leak with sf::Text?
Post by: alexis74 on April 26, 2017, 08:35:07 am
Hello, I have also find that there is a memory leak on the setOutlineThickness function.
I don't have any leaks without calling it (except the "still reachable: 115,181 bytes in 411 blocks"), but by calling it every frame I get these leak summary using valgrind.
==6109== Memcheck, a memory error detector
==6109== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6109== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==6109== Command: ./topkek
==6109==
==6109==
==6109== HEAP SUMMARY:
==6109==     in use at exit: 8,832,001 bytes in 8,403 blocks
==6109==   total heap usage: 71,083 allocs, 62,680 frees, 597,496,658 bytes allocated
==6109==
==6109== LEAK SUMMARY:
==6109==    definitely lost: 159,840 bytes in 1,998 blocks
==6109==    indirectly lost: 8,556,726 bytes in 5,993 blocks
==6109==      possibly lost: 254 bytes in 1 blocks
==6109==    still reachable: 115,181 bytes in 411 blocks
==6109==         suppressed: 0 bytes in 0 blocks
==6109== Rerun with --leak-check=full to see details of leaked memory
==6109==
==6109== For counts of detected and suppressed errors, rerun with: -v
==6109== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
 
Title: Re: 2.4.1 Memory leak with sf::Text?
Post by: Hapax on April 26, 2017, 09:34:11 pm
When I don't give the text an outline thickness I get no leak report.
Does this still happen if you specify a thickness of zero? How about 0.01f?