Memory leak when drawing with pangomm/cairomm (MSYS2)
- From: 廖骏轩 <mikeljx 126 com>
- To: "gtkmm-list gnome org" <gtkmm-list gnome org>
- Subject: Memory leak when drawing with pangomm/cairomm (MSYS2)
- Date: Tue, 22 Jun 2021 19:38:41 +0800 (CST)
I have just found out that the Windows version of my project has a massive memory leak, while on Linux it
seems alright. It uses pangomm and cairomm to display text, and I believe it’s similar to the examples in the
online book, “Programming with gtkmm4”. queue_draw() is called every 100 ms, and in a few minutes, the memory
usage would reach several GB. If I don’t call queue_draw(), the memory used stays around 100 MB.
Does anyone know what caused the memory leak? Thanks!
Here is what the code looks like:
--------------------------
#include "KbiDraw.h"
#include <glibmm/main.h>
#include <sstream>
KbiDraw::KbiDraw(Player* p_player)
:
player(p_player)
{
set_content_width(300);
set_content_height(300);
set_expand(true);
set_draw_func(sigc::mem_fun(*this, &KbiDraw::on_draw));
Glib::signal_timeout().connect(sigc::mem_fun(*this, &KbiDraw::refresh), 100);
}
KbiDraw::~KbiDraw()
= default;
void KbiDraw::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
{
// Draw a black rectangle
cr->set_source_rgb(0.0, 0.0, 0.0);
draw_rectangle(cr, width, height);
// and some white text
cr->set_source_rgb(1.0, 1.0, 1.0);
draw_text(cr, width, height);
Cairo::Matrix matrix(1.0, 0.0, 0.0, -1.0, 0.0, height);
// apply the matrix
cr->transform(matrix);
}
void KbiDraw::draw_rectangle(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
{
cr->rectangle(0, 0, width, height);
cr->fill();
}
void KbiDraw::draw_text(const Cairo::RefPtr<Cairo::Context>& cr, int rectangle_width, int rectangle_height)
{
Pango::FontDescription font;
font.set_family("Serif");
font.set_weight(Pango::Weight::BOLD);
font.set_size(35000);
std::stringstream draw_note;
auto current_notes = player->get_current_notes();
for (const auto& note : current_notes) {
draw_note << note << " ";
}
auto layout = create_pango_layout(draw_note.str());
layout->set_font_description(font);
int text_width;
int text_height;
//get the text dimensions (it updates the variables -- by reference)
layout->get_pixel_size(text_width, text_height);
// Position the text in the middle
cr->move_to((rectangle_width - text_width)/2, (rectangle_height - text_height)/2);
layout->show_in_cairo_context(cr);
}
bool KbiDraw::refresh()
{
queue_draw();
return true;
}
------------------------------------------------------
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]