gtk window moved then crash - gcc

I wanted to move my own gtk program from old MinGW to the latest one. I met a lot of questions in the past 2 weeks and couldn't find a way. Please refer to the other 2 questions I posted:
does mingw influence compilation a gtk program and compile gtk in mingw but failed.
Finally, I compiled gtk+2.24.10 by myself in MinGW. Followed are the details for environment:
GTK version gcc version
old mingw 2.10.11 -- from third party 3.4.5
new mingw 2.24.10 -- compiled by myself 4.7.2 or 4.6.2 -- I tried both
For the program in the old environment, everything runs well.
For the program in the new environment, the problem mentioned above "does mingw influence compilation a gtk program" disappeared. But there is a new problem I can't find any clue where it comes from:
When launching my program, if I move the GUI window, it crashed. I used gdb to debug, but if it crashed, system is frozen. The only way is to use "ctrl + alt + del" to activate task manager and stop the program. Then in GDB, where or bt full didn't show any useful information. If I don't move the GUI window, all the function in the window runs well. One more thing, my program uses openssl and pdflib which are from third party. The program links to the libs, but they are not used when the GUI window just is launched.
Is any bug in my own code? I don't think so because it runs well in the old MinGW environment. Some warnings about g_thread_create, g_thread_init is deprecated are in gtk+2.24.10 packages but don't show up in gtk+2.10.11 packages.
Is anything wrong with the gtk system, including gtk, glib, cario, atk etc., compiled by myself in MinGW? I can't tell. But gtk_demo included in gtk package runs well, I can move the program's window to wherever.
Is it related with gcc version? I can't tell. I googled and found someone complained that his gtk program complied by gcc 4.7.x is not stable and is affected by many sudden crashes. gcc 4.6.2 is absolutely suggested by someone. But in my case, it still crashed even compiled by gcc 4.6.2.
Is it related with openssl and pdflib which are from third party due to ABI issue? I can't tell.
M code is compiled in a Linux environment -- gtk+2.24.6. It does run well.
UPDATE 1:
I found some clue. The problem is from thread. But I still didn't know why it worked in the old MinGW but failed in the new MinGW. Followed is a simple example.
As I said, if I compile it in old MinGW, it worked -- the data, in fact a counter, is updated in the widget. And I can move the window.
If I compile it in new MinGW, it worked -- the data, in fact a counter, is updated in the widget. But if I move the window, it crashed. If I commented what are related with thread, then I can move the window, but data won't be updated in the window.
#include <gtk/gtk.h>
static int counter = 0;
static PangoLayout* layout;
static GdkGC* gc1;
static GdkGC* gc2;
//**/static GMutex* mu;
static gboolean on_expose_event(GtkWidget* widget, GdkEventExpose* event)
{
gchar the_string[20];
//**/g_mutex_lock(mu);
gdk_draw_rectangle(GDK_DRAWABLE(widget->window), gc1, TRUE, 0, 0, widget->allocation.width, widget->allocation.height);
snprintf(the_string, 20, "%d", counter);
pango_layout_set_text(layout, the_string, -1);
gdk_draw_layout(GDK_DRAWABLE(widget->window), gc2, 180, 120, layout);
//**/g_mutex_unlock(mu);
g_print (".");
return FALSE;
}
gpointer func(gpointer data)
{
//**/g_usleep(10000);
GdkWindow* window = GDK_WINDOW(data);
while(TRUE)
{
gdk_threads_enter();
gdk_window_invalidate_rect(window, NULL, FALSE);
gdk_threads_leave();
//**/gdk_window_process_updates(window, FALSE);
if(counter % 100 == 0) g_print("X");
g_usleep(10);
++counter;
}
return FALSE;
}
int main(int argc, char** argv)
{
GtkWidget* window;
GtkWidget* drawer;
GdkColormap* colormap;
GdkColor color;
g_thread_init(NULL);
gdk_threads_init();
gtk_init(&argc, &argv);
//:Create widgets and
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
drawer = gtk_drawing_area_new();
gtk_widget_set_size_request(drawer, 400, 300);
gtk_container_add(GTK_CONTAINER(window), drawer);
//.
gtk_widget_show_all(window);
//:Initializing Graphic Contexts
colormap = gtk_widget_get_colormap(GTK_WIDGET(drawer));
layout = gtk_widget_create_pango_layout(GTK_WIDGET(drawer), NULL);
gc1 = gdk_gc_new(GDK_DRAWABLE(GTK_WIDGET(drawer)->window));
gc2 = gdk_gc_new(GDK_DRAWABLE(GTK_WIDGET(drawer)->window));
gdk_color_parse("#000", &color);
gdk_colormap_alloc_color(colormap, &color, FALSE, TRUE);
gdk_gc_set_background(gc1, &color);
gdk_color_parse("#fff", &color);
gdk_colormap_alloc_color(colormap, &color, FALSE, TRUE);
gdk_gc_set_foreground(gc2, &color);
//.
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(drawer), "expose_event", G_CALLBACK(on_expose_event), NULL);
//**/mu = g_mutex_new();
//Run the problematic thread!
g_thread_create(func, GTK_WIDGET(drawer)->window, FALSE, NULL);
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
//**/g_mutex_free(mu);
return 0;
}
Compile:
gcc -g -o example update_widget.c `pkg-config --libs --cflags gtk+-2.0 gthread-2.0`
UPDATE 2:
I checked config.log of compilation of glib and found the words below:
configure:26678: WARNING: I can't find the MACRO to enable thread safety on your
platform (normally it's _REENTRANT). I'll not use any flag on
compilation now, but then your programs might not work.
Please provide information on how it is done on your system.
Does it influence the thread function?
My configure for compilation of glib is:
export CFLAGS="-march=i686"
./configure --prefix=$HOME/gtk+$GTK_VERSION --with-threads=win32 --with-pcre=internal
make
make install

Related

gcc-4.8.2 doesn't link pthread

all.
Compiling simple stuff using the gcc toolchain for several years, today I ran against a curious phenomenon.
I installed Kubuntu 14.04 to a common desktop i686 machine with gcc 4.8.2 in it. But then, trying to build some well coded stuff pulled out from my local repository, I ran against tons of 'undefined reference to' messages. The code compiles, links und runs well under Ubuntu 11.04 / gcc 4.5.2.
I checked the linking process (by -Wl,--verbose to gcc), think it works. It finds all libraries I specify in the link command. An objdump -t myLib.so brings exactly the symbols I'd expect - but the linker doesn't see them.
Checking the pthread library also brings according symbols, except they are suffixed with some #GLIBC... stuff. Didn't check linker/loader tricks so far.
A sample like
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
static void *fooo (void *xxx) {
char *txt = (char*)xxx;
printf("My job is to print this :'%s'. Bye now!\n", txt);
return 0;
}
int main (int argc, char *argv[]) {
pthread_t thd;
pthread_create(&thd, NULL, fooo, "A POSIX thread");
sleep(1);
return 0;
}
runs very well on the old system just saying
gcc -l pthread fooo.c && ./a.out
but breaks at the linking step with 4.8.2.
Any idea would be very welcome.
.M
Thanks to sfrehse, JoachimPileborg et al!
Indeed, success depends on argument order. I knew this fact for static linking, but it is new in processing of shared objects with gcc.
Does someone know what the background of this improvement is? It breaks innumerable build processes, and I guess thousands of tomatoes are being made ready against gcc.gnu.org .....
.M

Using C++11 with Cocos2d-x for Android

The beginning of my issue is that I'm trying to use regular expressions in Cocos2d-x. For whatever reason, std::tr1::regex isn't working with C++98, so I'm trying to use std::regex with C++11 (along with some other C++11 features). This is working with iOS now, since it's really easy to change the version of C++ in Xcode, but I'm having all kinds of trouble getting this to work on Android.
I'm using the r8e version of the NDK with the gnustl_static library. I set the LOCAL_CPPFLAGS += -std=c++11 I've tried setting the toolchain version to clang (in addition to the default). Regardless of the toolchain, I am now able to compile my code, but it still crashes when I try to create a std::regex object std::regex reg1("[a-z][0-3]*"); It seems like some people are able to get C++11 to work with the Android NDK expanded library (not the "minimal C++ runtime support library"), but I can't figure it out. I've read lots of ideas and I've tried most of them, and I've seen some clues, such as the following from CHANGES.html in the NDK docs:
Patched GCC 4.4.3/4.6/4.7 libstdc++ to work with Clang in C++11
I don't know enough about how this all fits together, so could someone point me in the right direction? What am I missing here?
Open your Application.mk file and add following two lines at the end:
APP_CPPFLAGS += -std=c++11
NDK_TOOLCHAIN_VERSION=4.7
Note: As you mentioned that you are using NDK's version r8e the toolchain version you need is 4.7. If it is r9, you can set it to 4.8.
Hope this helps.
Alternatively, if you aren't restricted to using c++'s std::regex, you could try using standard C: regcomp() and regexec() .
Here a sample implementation (http://pubs.opengroup.org/onlinepubs/009695399/functions/regcomp.html):
#include <regex.h>
int match(const char *string, const char *pattern)
{
int status;
regex_t re;
if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
return(0); /* Report error. */
}
status = regexec(&re, string, (size_t) 0, NULL, 0);
regfree(&re);
if (status != 0) {
return(0); /* Report error. */
}
return(1);
}
In Your android.mk add
LOCAL_CPPFLAGS += -std=gnu++0x

Compiling GWEN with CodeBlocks

so, I'm trying to compile Gwen in Windows, for use with a project I have coming up. I downloaded the source from Garry's GitHub, and followed his instructions on building the compilation before importing it to Code::Blocks to compile. I import the .cbp file, start compiling, and after a few minutes I get:
Error: '_asm' was not declared in this scope.
The error comes from some code after a line containing #ifdef _WIN32.
Exact file: gwen.cpp, line 49.
More information:
OS: Windows 7 64bit.
Compiler: Latest gcc from the MinGW, 4.7.2 (MinGW32)
I think it is because MinGW doesn't understand the assembler, it should be asm for that compiler. I think this is cause by using _WIN32 instead of WIN32. The former is the platform and the latter is API.
Try changing it to:
void AssertCheck(bool b, const char* strMsg)
{
if (!b)
{
Msg("Assert: %s\n", strMsg);
#ifdef WIN32
MessageBoxA(NULL, strMsg, "Assert", MB_ICONEXCLAMATION|MB_OK);
_asm { int 3 }
#endif
}
}
EDIT: Alternatively you could try Gwork, which is a tidied up version of GWEN.

Memory leak in GTK under Windows 7 in gtk_widget_queue_draw

The following GTK program:
#include <gtk/gtk.h>
GtkVBox *vbox;
GtkWindow *win;
gboolean Timer (gpointer user_data)
{
gtk_widget_queue_draw (GTK_WIDGET (vbox));
return 1;
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
win = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);
vbox = (GtkVBox*)gtk_vbox_new(TRUE, 1);
gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(vbox));
g_timeout_add (200, Timer, 0);
gtk_widget_show_all(GTK_WIDGET(win));
gtk_main();
return 0;
}
Leaks under Windows 7 64-bit when compiled using gcc 4.5.2 as the task manager shows. It does not leak when compiled under Windows XP 32-bit. The program does nothing. It creates a window, put there are box and then calls to gtk_widget_queue_draw from a timer each 200ms.
Here is a batch file to compile and link the test. The variable GTK is set to directory containing the binary distribution of GTK. E.g. 2.24.10 in this case.
set GTK=c:/temp/GTK
gcc -c -mms-bitfields -I%gtk%/lib/gtk-2.0/include -I%gtk%/lib/gdk-2.0/include -I%gtk%/lib/glib-2.0/include -I%gtk%/include -I%gtk%/include/atk-1.0 -I%gtk%/include/gdk-pixbuf-2.0 -I%gtk%/include/cairo -I%gtk%/include/pango-1.0 -I%gtk%/include/gio-win32-2.0 -I%gtk%/include/glib-2.0 -I%gtk%/include/gtk-2.0 test.c
gcc test.o --mwindows -Wl,-luuid -L%gtk%/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -lgobject-2.0 -lglib-2.0 -o test.exe
Any thoughts? What is broken here GTK, gcc, MinGW? Some compiler/linker options missed?
Windows 7 64-bit, using 32-bit MinGW and GTK+.
I have a similar case without the gtk_widget_queue_draw (GTK_WIDGET (vbox)); call in the Timer function.
My impression is that it is the g_timeout_add() that induce the memory leak (as reported on the process tab in the windows task manager)
PTL.
This was GTK bug 685959. Most of the leak was fixed in GTK 2.24.14.
Bug 707760 tracks the packages (binaries, bundle) update.

x11 code does not compile in Code Blocks

I am currently running the latest version of Code-blocks in Ubuntu 11.04.
I have GTK+2, and 3 developer libraries fully installed (and working), and presumably have x11 installed. The header files are there.
However, a simple code will not compile using x11 coding.
#include "X11/Xlib.h"
int main() {
Display *display = XOpenDisplay(0);
Window root = DefaultRootWindow(display);
XWarpPointer(display, None, root, 0, 0, 0, 0, 100, 100);
XCloseDisplay(display);
return 0;
}
This give me the readout of:
obj/Release/main.o||In function `main':|
undefined reference to `XOpenDisplay'
undefined reference to `XWarpPointer'
undefined reference to `XCloseDisplay'
|=== Build finished: 3 errors, 0 warnings ===|
I've tried reading multiple webpages of 'linking' x11, I only find headerfiles, and not the file type asked by the linker within the compiler (That's the wrong term for that.. it's not a compiler.. it's something else.. I know. Apologies)
I don't know what Code Blocks is, but for a normal compile/link process, you need to specify -lX11 to link with libX11.so for the Xlib functions.
Thanks! I just had the same problem. To spell it out:
Settings > Compiler and Debugger > Linker settings > Other linker options > "-lX11"
For the record 'codelite' got it right all by itself.
In CodeBlocks you can just set in the project options libraries to link against, setting -lX11 in the compiler settings will make every program you compile with codeblocks link against X11.

Resources