about textview (gtk_text_buffer_insert_with_tags cause segmentaion fault)
- From: "朱碧岑"<zhubicen gmail com>
- To: "gtk-app-devel-list"<gtk-app-devel-list gnome org>
- Subject: about textview (gtk_text_buffer_insert_with_tags cause segmentaion fault)
- Date: Tue, 15 Jul 2008 01:55:33 +0800
Hi all ,
When I insert some text to textview in a thread in the following code(myThread::run) . it will segmentation
fault .
Could someone help me to fix this issue.
Any comments and suggestion will be welcomed.
bzhu
#include <gtk/gtk.h>
#include <wchar.h>
#include <string.h>
#include <pthread.h>
GtkWidget* tv;
GtkTextTag* tag;
#include <assert.h>
class Thread
{
public:
Thread();
void start();
virtual void run();
private:
pthread_t m_hThread;
};
Thread::Thread()
{
}
static void* _ou_thread_proc(void* param)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
Thread* tp = (Thread*)param;
tp->run();
pthread_exit(NULL);
return NULL;
}
void Thread::start()
{
int iret = pthread_create( &m_hThread, NULL, _ou_thread_proc,this);
assert(iret == 0);
}
void Thread::run()
{
}
class myThread:public Thread
{
public :
void run()
{
g_print("Thread is running\n");
//Code belowe will cause the segmentation fault
gdk_threads_enter();
GtkTextBuffer* tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
GtkTextIter end;
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(tb),&end);
gchar text[] = "Thread is running\n";
gtk_text_buffer_insert_with_tags(GTK_TEXT_BUFFER(tb),&end,text,-1,tag);//segmentaion fault.
gdk_threads_leave();
}
};
void* thread_proc(void* param)
{
gdk_threads_enter();
GtkTextBuffer* tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
GtkTextIter end;
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(tb),&end);
gchar text[] = "Thread is running\n";
gtk_text_buffer_insert_with_tags(GTK_TEXT_BUFFER(tb),&end,text,-1,tag);
gdk_threads_leave();
}
myThread g_thread;
void go_click(GtkWidget* goButton,GtkWidget* noWidget)
{
//g_print("You click");
//pthread_t mythread;
//pthread_create(&mythread,NULL,thread_proc,NULL);
g_thread.start();
}
int main(int argc,char* argv[])
{
GtkWidget* window;
g_thread_init(NULL);
gdk_threads_init();
gdk_threads_enter();
gtk_init(&argc,&argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget* vbox = gtk_vbox_new(FALSE,2);
gtk_container_add(GTK_CONTAINER(window),vbox);
tv = gtk_text_view_new();
gtk_box_pack_start(GTK_BOX(vbox),tv,TRUE,TRUE,0);
gtk_widget_set_size_request(tv,160,160);
GtkWidget* go = gtk_button_new_with_label("go");
gtk_box_pack_start(GTK_BOX(vbox),go,TRUE,TRUE,0);
g_signal_connect (G_OBJECT (go),"clicked",G_CALLBACK (go_click),NULL);
//write a text to the text view
GtkTextBuffer* tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
tag = gtk_text_buffer_create_tag (tb, "PASSTAG","scale",PANGO_SCALE_XX_LARGE,NULL);
GtkTextIter end;
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(tb),&end);
gchar text[] = "just a test\n";
gtk_text_buffer_insert_with_tags(GTK_TEXT_BUFFER(tb),&end,text,-1,tag);
gtk_widget_show_all(window);
gtk_main();
gdk_threads_leave();
return 1;
}
Makefile:
CC = g++
RM = rm
GTK_CFLAGS = `pkg-config --cflags gtk+-2.0 gthread-2.0 `
GTK_LIBS = `pkg-config --libs --cflags gtk+-2.0 gthread-2.0 `
CFLAGS = -g -Wall
LIBS = -lpthread
.SUFFIXES:.cpp
SRCS = textview.cpp
#thread.cpp
OBJS = $(SRCS:.cpp=.o)
.cpp.o:
$(CC) $(CFLAGS) $(GTK_CFLAGS) -c $<
tv: $(OBJS)
$(CC) $(GTK_LIBS) $(LIBS) -o $@ $(OBJS)
clean:
$(RM) *.o
$(RM) tv
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]