简体   繁体   中英

Seg fault in cairo when using some static libraries

I have the following routine for taking a screenshot:

void take_screenshot(const std::string & file_name)
{
  Display *disp;
  Window root;
  cairo_surface_t *surface;
  int scr;

  disp = XOpenDisplay(nullptr);
  scr = DefaultScreen(disp);
  root = DefaultRootWindow(disp);

  surface = cairo_xlib_surface_create(disp, root, DefaultVisual(disp, scr),
                      DisplayWidth(disp, scr), 
                      DisplayHeight(disp, scr));
  cairo_surface_write_to_png(surface, file_name.c_str()); // <-- here is the seg fault
  cairo_surface_destroy(surface);
}

This routine was taken from the answer of https://stackoverflow.com/users/436275/uli-schlachter to this question Making a screenshot using Xlib and Cairo libs [fail] (credits to the authors)

The routine is part of a program called collector . which is linked as follows:

clang -o collector -g -O2 -fno-strict-aliasing collector.o common-vars.o \
collector-events.o listeners.o active_window.o \
../lib/libperfq-common.a -lPocoNet -lPocoFoundation -luiohook\
-lstdc++ -lgsl -lgslcblas -lm -lcairo -lXt -lXtst -lXinerama \
-lX11 -lpthread -lc

So far so good. The program works very well.

However, when I statically link the poco and uiohook libraries, the program throws a seg fault at the line indicated above.

The executable with poco and uiohook libraries statically linked is produced as follows:

clang -o collector -g -O2 -fno-strict-aliasing collector.o \
common-vars.o collector-events.o listeners.o active_window.o \
../lib/libperfq-common.a /home/lrleon/poco/lib/libPocoNet.a \
/home/lrleon/poco/lib/libPocoFoundation.a \
/home/lrleon/uiohook/lib/libuiohook.a -lstdc++ -lgsl -lgslcblas \
-lm -lcairo -lXt -lXtst -lXinerama -lX11 -lpthread -lc

With this version, I have a segfault. And after switching some dynamic libraries, watch with a debugger, I do know what is causing the error.

I guess the fact that some libraries statically chained make the call to cairo_surface_write_to_png() fails. But I have no idea why.

Any idea? Thanks in advance

I did just patch a crash on linux due to xcb in the 2.1 branch. That may fix your issue when 2.1.1. I would recommend trying 2.0 which does not contain xcb to see if that fixes your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM