0

I have been trying to make a program that takses the active window, and displays it in its window. I have successfully exceeded my goal. But the problem is, it uses a lot of ram, and it keeps using more every frame update(20fps). Here is the source code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

int funcfinished = 1;

GtkWidget *window;

GdkPixbuf *fupdate_pixbuf;
GtkStyle *fupdate_style;
GdkPixmap *fupdate_background;
gint fupdate_xorig;
gint fupdate_yorig;
gint fupdate_width;
gint fupdate_height;
GdkPixbuf *fupdate_screenshot;
GdkWindow *fupdate_window;

gboolean frameupdate()
{
    if(funcfinished == 1)
    {
        /*********[FuncFinish]*********/
        funcfinished = 0;
        fupdate_pixbuf = NULL;
        fupdate_style = NULL;
        fupdate_background = NULL;
        fupdate_screenshot = NULL;
        fupdate_window = NULL;
        fupdate_xorig = 0;
        fupdate_yorig = 0;
        fupdate_width = 0;
        fupdate_height = 0;

        /*********[Func]*********/
        fupdate_window = gdk_screen_get_active_window(gdk_screen_get_default());

        gdk_drawable_get_size(fupdate_window, &fupdate_width, &fupdate_height);

        fupdate_pixbuf = gdk_pixbuf_get_from_drawable(NULL, fupdate_window, NULL, 0, 0, 0, 0, fupdate_width, fupdate_height);

        gdk_pixbuf_render_pixmap_and_mask(fupdate_pixbuf, &fupdate_background, NULL, 0);
        fupdate_style = gtk_style_new();
        fupdate_style->bg_pixmap[0] = fupdate_background;
        gtk_widget_set_style(GTK_WIDGET(window), GTK_STYLE(fupdate_style));

        /*********[FuncFinish]*********/
        fupdate_pixbuf = NULL;
        fupdate_style = NULL;
        fupdate_background = NULL;
        fupdate_screenshot = NULL;
        fupdate_window = NULL;
        fupdate_xorig = 0;
        fupdate_yorig = 0;
        fupdate_width = 0;
        fupdate_height = 0;
        funcfinished = 1;
    }
    else
    {
        printf("Skipped 1 frame update");
    }
    return TRUE;
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Hay Day Autobot");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);


    g_signal_connect(window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    g_timeout_add(50, frameupdate, 0);

    gtk_widget_show(window);

    gtk_main();
    return 0;
}

I also made a video of it in action, showing off the problem: https://www.youtube.com/watch?v=GNCwNetLLBM

1 Answers1

0

You are not releasing the memory that you create during the frame update function. For each function that you use there, you should look it up in the documentation and see what it says under "return value".

For example, gdk_screen_get_active_window() lists its return value as "transfer full". That means that "full" ownership of the return value is "transferred" to you when you call that function; ownership means that you are responsible for freeing the memory. Usually the documentation will also say how to do that. In this case you can read

The returned window should be unrefed using g_object_unref() when no longer needed.

On the other hand, gdk_screen_get_default() is "transfer none", so you don't need to do anything there.

ptomato
  • 56,175
  • 13
  • 112
  • 165