GTK+中文社区(gtk.awaysoft.com)

 找回密码
 马上加入

QQ登录

只需一步,快速开始

查看: 3128|回复: 0

求教。gtk内存错误

[复制链接]

该用户从未签到

发表于 2012-2-28 12:27:11 | 显示全部楼层 |阅读模式
  1. #include <gtk/gtk.h>
  2. #include <glib.h>
  3. #include <gio/gio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define PORT 5000
  7. #define IP "172.21.128.219"
  8. #define BUFMAX 1024
  9. GtkBuilder *builder;
  10. GtkWidget *ImageCon;
  11. GtkWidget *ImageDis;
  12. GList *ip_list;
  13. GThread *thread_id;
  14. GSocket *socket;
  15. GSocketClient *client_socket;
  16. GInetAddress *client_addr;
  17. GSocketAddress * sock_addr;
  18. GSocketConnection *sock_con;
  19. GSocketConnectable *ser_sock;
  20. GError *sock_err;
  21. ///////////////////////////////////////////////////////////
  22. void Display(gchar * buf)
  23. {
  24. GtkTextBuffer *buf_rcv;
  25. GtkTextIter buf_rcv_start;
  26. GtkTextIter buf_rcv_end;
  27. buf_rcv=GTK_TEXT_BUFFER(gtk_builder_get_object(builder,"textbuffer_rcv"));
  28. gtk_text_buffer_get_start_iter(buf_rcv,&buf_rcv_start);
  29. gtk_text_buffer_get_end_iter(buf_rcv,&buf_rcv_end);
  30.   gtk_text_buffer_insert(buf_rcv,&buf_rcv_end,"服务器:\t",-1);
  31. // buf_rcv_end=0;
  32. // gtk_text_buffer_get_end_iter(buf_rcv,&buf_rcv_end);
  33. gtk_text_buffer_insert(buf_rcv,&buf_rcv_end,buf,-1);
  34. g_free(buf_rcv);
  35. }
  36. ///////////////////////////////////////////////////////////
  37. void Receive()
  38. {
  39. gchar  buf[BUFMAX]={0};
  40. gint size;
  41. while (1)
  42. {
  43.   size=g_socket_receive(socket,buf,BUFMAX,NULL,NULL);
  44.   if (size!=-1)
  45.   {
  46.    g_print("%d\n",size);
  47.    g_print("%s\n",buf);
  48.    Display(buf);
  49.    memset(buf,0,BUFMAX);
  50.   }
  51. }
  52. }
  53. //////////////////////////////////////////////////////////
  54. gboolean Connect(char * ip,char *port )
  55. {
  56. /* address=socket_address_from_string(G_SOCKET_FAMILY_IPV4);
  57. client.sa_family=AF_INET;
  58. client.sin_port=htons(PORT);//1234ÊǶ˿ںÅ
  59. client.sin_addr.s_addr=inet_addr("192.168.0.1");
  60.   socket=g_socket_new(G_SOCKET_FAMILY_IPV4,G_SOCKET_TYPE_STREAM,G_SOCKET_PROTOCOL_TCP,&sock_err);
  61. g_socket_bind(socket,);*/
  62. client_socket=g_socket_client_new();
  63. client_addr=g_inet_address_new_from_string(IP);
  64. sock_addr=g_inet_socket_address_new(client_addr, PORT);
  65. ser_sock=g_network_address_new(ip,atoi(port));
  66. sock_con=g_socket_client_connect(client_socket,ser_sock,NULL,&sock_err);
  67. socket=g_socket_connection_get_socket(sock_con);
  68. g_socket_set_timeout(socket,1);
  69. g_object_unref(client_socket);
  70. g_object_unref(client_addr);
  71. g_object_unref(sock_addr);
  72. g_object_unref(ser_sock);
  73. thread_id=g_thread_create((GThreadFunc)Receive,NULL,TRUE,NULL);
  74. if (sock_err==NULL)
  75. {
  76.   g_print("%s!\n","连接成功");
  77.   return TRUE;
  78. }
  79.   else
  80. {
  81.   g_print("%s\n",sock_err->message);
  82.   g_error_free(sock_err);
  83.   return FALSE;
  84. }
  85. }
  86. //////////////////////////////////////////////////////////////
  87. void DisCon()
  88. {
  89. while (g_socket_is_closed (socket))
  90. {
  91.   g_socket_close(socket,NULL);
  92.   g_object_unref(socket);
  93. }
  94. g_thread_join(thread_id);
  95. g_print("%s!\n","断开连接");
  96. }
  97. ////////////////////////////////////////////////////////////
  98. void on_button_quit_clicked (GtkObject *object, gpointer user_data)
  99. {
  100. g_object_unref (G_OBJECT (builder));
  101. gtk_main_quit();
  102. }
  103. ///////////////////////////////////////////////////////////
  104. void on_togglebutton_con_toggled(GtkObject *object,gpointer user_data)
  105. {
  106. gchar * temp;
  107. if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (object)))
  108. {
  109.   g_print("%s\n","pressed!");
  110.   temp=gtk_entry_get_text(GTK_ENTRY(gtk_builder_get_object (builder,"comboboxentry-entry")));//GTK_COMBO(user_data)->
  111.   Connect(strtok(temp,":"),temp+strlen(strtok(temp,":"))+1);
  112.   gtk_button_set_label (GTK_BUTTON(object),"断开");
  113.   ImageDis=gtk_image_new_from_stock("gtk-disconnect",GTK_ICON_SIZE_BUTTON);
  114.   gtk_button_set_image(GTK_BUTTON(object),ImageDis);
  115.   g_free(ImageDis);
  116. }
  117. else
  118. {
  119.   g_print("%s\n","release!");
  120.   DisCon();
  121.   gtk_button_set_label (GTK_BUTTON(object), "连接");
  122.   ImageCon=gtk_image_new_from_stock("gtk-connect",GTK_ICON_SIZE_BUTTON);
  123.   gtk_button_set_image(GTK_BUTTON(object),ImageCon);
  124.   g_free(ImageCon);
  125. }
  126. g_free(temp);
  127. }
  128. void on_button_snd_clicked(GtkObject *object,gpointer user_data)
  129. {
  130. /* buf_snd_count++;
  131. gtk_text_buffer_get_iter_at_line_index (buf_snd,&buf_snd_start,buf_snd_line,0);
  132. gtk_text_buffer_get_end_iter(buf_snd,&buf_snd_end);
  133. if (gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,TRUE))
  134. {
  135.   if (0==buf_snd_count)
  136.    str_snd=g_string_new("");
  137.   g_assert (str_snd != NULL);
  138.   g_string_append_printf(str_snd,"%d:%s\n",buf_snd_count,gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,TRUE));
  139.   g_print("%s\n",str_snd->str);
  140.   gtk_text_buffer_set_text (buf_snd,str_snd->str,-1);
  141. }
  142. buf_snd_line=gtk_text_buffer_get_line_count(buf_snd);*/
  143. gint size;
  144. GtkTextBuffer *buf_snd;
  145. GtkTextBuffer *buf_rcv;
  146. GtkTextIter buf_snd_start;
  147. GtkTextIter buf_snd_end;
  148. GtkTextIter buf_rcv_start;
  149. GtkTextIter buf_rcv_end;
  150. gchar *temp;
  151. GSocketAddress *ser_addr;
  152. ser_addr=g_socket_get_remote_address (socket,NULL);
  153. buf_snd=GTK_TEXT_BUFFER(gtk_builder_get_object(builder,"textbuffer_snd"));
  154. buf_rcv=GTK_TEXT_BUFFER(gtk_builder_get_object(builder,"textbuffer_rcv"));
  155. gtk_text_buffer_get_start_iter(buf_rcv,&buf_rcv_start);
  156. gtk_text_buffer_get_end_iter(buf_rcv,&buf_rcv_end);
  157. gtk_text_buffer_get_start_iter(buf_snd,&buf_snd_start);
  158. gtk_text_buffer_get_end_iter(buf_snd,&buf_snd_end);
  159. temp=gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,FALSE);
  160. temp=strcat(temp,"\n");
  161. temp=strcat("客户端:\t",temp);
  162. gtk_text_buffer_insert(buf_rcv,&buf_rcv_end,temp,-1);
  163. size=g_socket_send_to(socket,ser_addr,gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,FALSE),strlen(gtk_text_buffer_get_text(buf_snd,&buf_snd_start,&buf_snd_end,FALSE)),NULL,NULL);
  164. g_print("%d\n",size);
  165. gtk_text_buffer_set_text(buf_snd,"",-1);
  166. gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(builder,"button_snd")),FALSE);
  167. g_free(buf_snd);
  168. g_free(buf_rcv);
  169. g_free(temp);
  170. }
  171. ////////////////////////////////////////////////////////////////////////
  172. void on_textbuffer_changed(GtkObject *object,gpointer user_data)
  173. {
  174. gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(builder,"button_snd")),TRUE);
  175. }
  176. ///////////////////////////////////////////////////////////////////////
  177. int main (int argc, char *argv[])
  178. {
  179. GtkWidget *window;
  180. gtk_init (&argc, &argv);      
  181. builder = gtk_builder_new ();
  182. gtk_builder_add_from_file (builder, "client.xml", NULL);
  183. window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
  184. // buf_snd=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtk_builder_get_object(builder,"textview_snd")));
  185. // buf_rcv=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtk_builder_get_object(builder,"textview_rcv")));
  186. gtk_builder_connect_signals (builder, NULL);         
  187. gtk_widget_show(window);      
  188. gtk_main ();
  189. return 0;
  190. }
复制代码
编译命令:

file:///C:/Program%20Files/Tencent/QQ/Users/337927455/Image/V{UP2B$MWI@M6~4IFNG2G0O.jpg
单步:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?马上加入

x
*滑块验证:
您需要登录后才可以回帖 登录 | 马上加入

本版积分规则

申请友链|Archiver|小黑屋|手机版|GTK+中文社区 ( 粤ICP备13080851号 )

我要啦免费统计

GMT+8, 2024-12-22 13:54 , Processed in 0.042541 second(s), 8 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表