3
votes

Sorry for my bad English.

I am learning gtk+ 3 as a "helloworld" by following this article https://developer.gnome.org/gtk3/stable/gtk-getting-started.html

My environment:
* Mac OS X 10.8.3
* XQuartz 2.7.4
* GTk+ 3.4.4
* i686-apple-darwin11-llvm-gcc-4.2

Everything worked fine before I tried to invoke this function as a signal handler for drawing:

static gboolean configure_event_cb(GtkWidget* widget,GdkEventConfigure* event,gpointer data)
{

    if(surface) cairo_surface_destroy(surface);

    surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget),
                                                CAIRO_CONTENT_COLOR,
                                                gtk_widget_get_allocated_width(widget),
                                                gtk_widget_get_allocated_height(widget));

    //非对象类型.看做普通的struct. cairo看做类似Graphics的静态类.
    cairo_t* cr = cairo_create(surface);

    cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);

    cairo_paint(cr);

    cairo_destroy(cr);

    return TRUE;
}

The program always crashed on the line "cairo_paint(cr)" or "cairo_fill" in other functions. Here is the callstack:

libcairo.2.dylib`_cairo_default_context_in_clip:
0x100a71ff5:  pushq  %rbp
0x100a71ff6:  movq   %rsp, %rbp
0x100a71ff9:  pushq  %rbx
0x100a71ffa:  pushq  %rax
0x100a71ffb:  movq   %rsi, %rbx
0x100a71ffe:  movq   40(%rdi), %rdi
0x100a72002:  callq  0x100a761c1               ; _cairo_gstate_in_clip
0x100a72007:  movl   %eax, (%rbx)
0x100a72009:  xorl   %eax, %eax
0x100a7200b:  addq   $8, %rsp
0x100a7200f:  popq   %rbx
0x100a72010:  popq   %rbp
0x100a72011:  ret    

Could somebody help me?

2

2 Answers

1
votes

I suspect memory corruption due to the call to cairo_surface_destroy. Where are you declaring surface? Where are you setting it before the call to cairo_surface_destroy? Are you sure it is pointing to a valid surface object with reference count > 0?

I'd try commenting that line out and seeing if you still get the crashes. You'll probably have some memory leaks to fix then, but at least you'll know if that's the problem.

0
votes

I've found the answer.

The actual problem is , the Cairo library wasn't loaded at runtime. The clue is that Xcode showed me following msg when I tried to draw something with cairo:
error: address doesn't contain a section that points to a section in a object file

So I changed my gcc command by adding -L/usr/local/lib -lcairo:

/usr/bin/gcc -Wall ./*.c -L/usr/local/lib -lcairo pkg-config --cflags --libs gtk+-3.0

After doing this, it doesn't crash anymore. Howerver, I don't know which type of library is actually linked to the executable, shared or static library? And why didn't it load that library at runtime?