armadillu / ofxFontStash

Easy (and fast) unicode string rendering addon for OpenFrameworks. FontStash is made by Andreas Krinke and Mikko Mononen
http://uri.cat/
91 stars 42 forks source link

any tips to solve memory leaks in destructor? #31

Open moebiussurfing opened 4 years ago

moebiussurfing commented 4 years ago

hey @armadillu ,

I am back into messing around into "my fly reloading fonts problems" as we talked here: https://github.com/armadillu/ofxFontStash2/issues/12

I found that the memory leak problem is here in ofxFontStash. (I am using both ofxFontStash and ofxFontStash2 together into the same project, and I have not checked yet the ofxFontStash2 destructor alone...)

Can you tell me some tips to check around it to try to solve this? What are the bigger difficulties or some starting point?

I made a very simple project to compare with ofTrueTypeFont class, and it works perfectly, no memory growing at all.

Here I paste a screenshot (5GB in 2 minutes reloading the font 5 times per second) and the code I used:

ofxFontStash

ofApp.h

#pragma once

#include "ofMain.h"
#include "ofxFontStash.h"

class ofApp : public ofBaseApp{

public:

    void setup();
    void draw();
    void keyPressed(int key);

    string s = "Font Example\n MYFONT\n EXPLORER\nAND DESTRUCT";

    ofxFontStash fontStash;
    ofTrueTypeFont fontOF;

    bool bUse_OF = true;

    float fontSize = 10;

    int tElap;
    int iFont = 0;
    vector <std::string> fPaths;

    //destruct ofxFontStash object and re create a new one
    void reload();
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    ofSetFrameRate(20);

    //font file paths
    fPaths.push_back("verdana.ttf");
    fPaths.push_back("frabk.ttf");
    fPaths.push_back("Arial Unicode.ttf");
    fPaths.push_back("VeraMono-Bold.ttf");
    fPaths.push_back("verdana.ttf");

    //load default
    fontOF.load("verdana.ttf", 14, true, true);
    fontStash.setup("verdana.ttf", 1.0, 1024, false, 8, 1.5);
}

//--------------------------------------------------------------
void ofApp::draw(){

    //auto trig destructor every 1000ms
    if (ofGetElapsedTimeMillis() - tElap > 1000)
    {
        reload();
    }

    int x = 5;
    int y = ofGetHeight() *0.5;
    ofSetColor(225);

    if (bUse_OF)
    {
        fontOF.drawString(s, x, y);
    }
    else
    {
        fontStash.drawMultiLine( s, fontSize, x, y, OF_ALIGN_HORZ_LEFT, 500);
    }
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

    //trig manually
    if (key == ' ')
    {
        reload();
    }

    //switch ofTrueTypeFont/fontStash
    if (key == 'f')
    {
        bUse_OF = !bUse_OF;
        cout << "using: "<< (bUse_OF ? "ofTrueTypeFont":"ofxfontStash") << endl;
    }
}

//--------------------------------------------------------------
void ofApp::reload()
{

    iFont++;
    iFont = iFont % fPaths.size();
    cout << iFont << endl;

    if (bUse_OF)
    {
        fontOF = ofTrueTypeFont();
        fontOF.load(fPaths[iFont], ofRandom(50,100), true, true);
        fontOF.setLineHeight(ofRandom(10,100.0f));
        fontOF.setLetterSpacing(ofRandom(0.4, 4.0));
    }
    else
    {
        fontStash = ofxFontStash();
        fontStash.setup(fPaths[iFont], 1.0, 1024, false, 8, 1.5);
        //fontStash.addFont("frabk.ttf");
        fontStash.setCharacterSpacing(ofRandom(20));
        fontStash.setLineHeight(ofRandom(50, 200));
        fontSize = ofRandom (10, 100);
    }

    tElap = ofGetElapsedTimeMillis();
}
moebiussurfing commented 4 years ago

hey @armadillu ,

There's another fontStash addon by @andorxornot that solves the destructor thing on the last two commits: https://github.com/Kiberchaika/ofxFontStash3/commits/master

I tested and it works perfectly. Not sure if it can help to modify yours, I'll check around.

armadillu commented 4 years ago

thanks makes sense, ill have a look!

moebiussurfing commented 4 years ago

Hey @armadillu, any chance to get this solved? Or it's hard to solve or not useful enough maybe?