100分请高手来帮忙亚 ...........................
#include < gtk/gtk.h>
#include < glib.h>
#include < glib/gprintf.h>
#include < unistd.h>
#include < stdlib.h>
#include < string.h>
#include < time.h>
void do_capture_screenshot (char *file, int sec)
{
gint x, y;
GdkScreen* cur_screen = NULL;
GdkWindow* window = NULL;
GdkPixbuf* pixbuf_screenshot = NULL;
GdkRectangle rect;
GdkRectangle screen_rect;
g_printf( "ready to capture.....\r\n ");
sleep(sec);
if (cur_screen == NULL)
cur_screen = gdk_screen_get_default ();
screen_rect.x = 0;
screen_rect.y = 0;
screen_rect.width = gdk_screen_get_width (cur_screen);
screen_rect.height = gdk_screen_get_height (cur_screen);
window = gdk_screen_get_root_window (cur_screen);
gdk_window_get_origin (window, &x, &y);
rect.x = x;
rect.y = y;
gdk_drawable_get_size (GDK_DRAWABLE (window), &rect.width,
&rect.height);
if (! gdk_rectangle_intersect (&rect, &screen_rect, &rect)) {
g_print( "Capture failed!...\r\n ");
return;
}
pixbuf_screenshot = gdk_pixbuf_get_from_drawable (NULL, window,
NULL,rect.x - x, rect.y - y, 0, 0,
rect.width, rect.height);
gdk_pixbuf_save (pixbuf_screenshot, file, "jpeg ", NULL, "quality ",
"100 ", NULL);
g_object_unref (pixbuf_screenshot);
g_print( "Capture saved!!!\r\n ");
return;
}
int main( int argc, char *argv[])
{
int cmd, sec;
char *file = NULL;
time_t tt;
struct tm *tm_ptr = NULL;
GtkWidget *window;
file = (char *)malloc(32);
time(&tt);
tm_ptr = localtime(&tt);
strftime(file, 32, "%Y%m%d%H%M%S ", tm_ptr);
strcat(file, ".jpg ");
sec = 3;
gtk_init (&argc, &argv);
while ((cmd = getopt(argc, argv, "o:s: ")) != -1) {
switch (cmd) {
case 'o ':
file = optarg;
break;
case 's ':
sec = atoi(optarg);
break;
default:
break;
}
}
do_capture_screenshot(file, sec);
return 0;
}
这是个抓屏的程序,我每隔一秒调用他,但是每次内存都没有释放.
pixbuf_screenshot = gdk_pixbuf_get_from_drawable (NULL, window,
NULL,rect.x - x, rect.y - y, 0, 0,
rect.width, rect.height);
每次执行到这里时,都重新要分配内存,而原先的又不释放....
怎么回事呀...高手指点一下呀.谢谢了
[解决办法]
GdkPixbuf* gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
GdkDrawable *src,
GdkColormap *cmap,
int src_x,
int src_y,
int dest_x,
int dest_y,
int width,
int height);
Transfers image data from a GdkDrawable and converts it to an RGB(A) representation inside a GdkPixbuf. In other words, copies image data from a server-side drawable to a client-side RGB(A) buffer. This allows you to efficiently read individual pixels on the client side.
If the drawable src has no colormap (gdk_drawable_get_colormap() returns NULL), then a suitable colormap must be specified. Typically a GdkWindow or a pixmap created by passing a GdkWindow to gdk_pixmap_new() will already have a colormap associated with it. If the drawable has a colormap, the cmap argument will be ignored. If the drawable is a bitmap (1 bit per pixel pixmap), then a colormap is not required; pixels with a value of 1 are assumed to be white, and pixels with a value of 0 are assumed to be black. For taking screenshots, gdk_colormap_get_system() returns the correct colormap to use.
If the specified destination pixbuf dest is NULL, then this function will create an RGB pixbuf with 8 bits per channel and no alpha, with the same size specified by the width and height arguments. In this case, the dest_x and dest_y arguments must be specified as 0. If the specified destination pixbuf is not NULL and it contains alpha information, then the filled pixels will be set to full opacity (alpha = 255).
If the specified drawable is a pixmap, then the requested source rectangle must be completely contained within the pixmap, otherwise the function will return NULL. For pixmaps only (not for windows) passing -1 for width or height is allowed to mean the full width or height of the pixmap.
If the specified drawable is a window, and the window is off the screen, then there is no image data in the obscured/offscreen regions to be placed in the pixbuf. The contents of portions of the pixbuf corresponding to the offscreen region are undefined.
If the window you 're obtaining data from is partially obscured by other windows, then the contents of the pixbuf areas corresponding to the obscured regions are undefined.
If the target drawable is not mapped (typically because it 's iconified/minimized or not on the current workspace), then NULL will be returned.
If memory can 't be allocated for the return value, NULL will be returned instead.
(In short, there are several ways this function can fail, and if it fails it returns NULL; so check the return value.)
This function calls gdk_drawable_get_image() internally and converts the resulting image to a GdkPixbuf, so the documentation for gdk_drawable_get_image() may also be relevant.
dest : Destination pixbuf, or NULL if a new pixbuf should be created.
src : Source drawable.
cmap : A colormap if src doesn 't have one set.
src_x : Source X coordinate within drawable.
src_y : Source Y coordinate within drawable.
dest_x : Destination X coordinate in pixbuf, or 0 if dest is NULL.
dest_y : Destination Y coordinate in pixbuf, or 0 if dest is NULL.
width : Width in pixels of region to get.
height : Height in pixels of region to get.
Returns : The same pixbuf as dest if it was non-NULL, or a newly-created pixbuf with a reference count of 1 if no destination pixbuf was specified, or NULL on error
[解决办法]
先释放原来空间,然后调用 gdk_pixbuf_get_from_drawable :
if(pixbuf_screenshot != NULL)
free(pixbuf_screenshot); //先释放原来的空间
pixbuf_screenshot = gdk_pixbuf_get_from_drawable (NULL, window,
NULL,rect.x - x, rect.y - y, 0, 0,
rect.width, rect.height);
[解决办法]
if(pixbuf_screenshot != NULL) //先进行释放判断 !
free(pixbuf_screenshot); //先释放原来的空间
如果还是不行,
检查你的程序逻辑!!
看看错误提示是什么!
[解决办法]
用完以后要用g_object_unref把这短内存的引用计数减掉1,然后再释放。
否则会报错的。