我有一个尝试执行以下操作的应用程序:
- 创建
GTK2
顶级主窗口 - 在主窗口中添加一个固定框架,用于小部件的绝对定位
- 创建一个矩阵,
GtkImages
用于显示动画GIFS
和静态JPEGS
- 在启动时,
JPEGS
从列表中随机选取的静态将填充矩阵 - 当事件发生时,矩阵将充满动画
GIFS
- 动画结束时可能
JPEGS
会再次在矩阵中显示不同
仅当随机选择的两个或多个JPEGS
被放置在矩阵的一行中时,才会发生运行时错误。
以下是此类运行时错误的示例:
(wrong:3909): Gtk-WARNING **: Can't set a parent on widget which has a parent
如果该行的每个图像都是唯一的,则不会发生运行时错误。
代码片段和运行时输出如下:
/*
* Compile me with:
* gcc -Wall -o wrong wrong.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
*/
/* header includes */
/**** prototypes ****/
/********************/
typedef struct
{
unsigned int pixel_width, pixel_height;
gchar fileName[20];
GtkWidget *image;
}symbol_t;
symbol_t symbols[] =
{
{ 118, 107, "images/LO.jpg", NULL },
{ 118, 107, "images/L1.jpg", NULL },
{ 118, 107, "images/L2.jpg", NULL },
{ 118, 107, "images/L3.jpg", NULL },
{ 118, 107, "images/H1.jpg", NULL },
{ 118, 107, "images/H2.jpg", NULL },
{ 118, 107, "images/H3.jpg", NULL },
{ 118, 107, "images/H4.jpg", NULL },
{ 118, 107, "images/H5.jpg", NULL }
};
GtkWidget *frame; /* for absolute positioning of widgets */
GtkWidget *window;
int Init( void )
{
/* initialize random number generator */
}
static void destroy (GtkWidget *window, gpointer data)
{
gtk_main_quit ();
}
GtkWidget *SetupWindow(gchar *data, const gchar *filename)
{
/* setup top-level window setting the background to the image contained
in *filename and return window widget
*/
return(window);
}
int main (int argc, char *argv[])
{
unsigned int y, i, pos_x, pos_y;
gtk_init (&argc, &argv);
Init(); // init random number generator
window = SetupWindow("Broken", "images/background.jpg");
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);
/* setup symbol jpgs */
for( i = 0; i < 9; i++ )
{
/* load each symbol image into memory */
symbols[i].image = gtk_image_new_from_file( symbols[i].fileName );
}
/* display some symbols */
pos_y = 150;
pos_x = 187;
for( y = 0; y < 5 ; y++ ) /* first row - 5 symbols */
{
i = (unsigned int)(random()%9);
printf("Symbol[%d] [%s]\n", i, symbols[i].fileName);
gtk_fixed_put(GTK_FIXED(frame), symbols[i].image, pos_x, pos_y);
pos_x += symbols[i].pixel_width;
}
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
将两个或多个匹配符号(图像)放在行上时的运行时错误:
[chim] ~/source/matrix > ./wrong Symbol[1] [images/L1.jpg] Symbol[7] [images/H4.jpg] Symbol[0] [images/LO.jpg] Symbol[7] [images/H4.jpg] (wrong:3909): Gtk-WARNING **: Can't set a parent on widget which has a parent Symbol[5] [images/H2.jpg] (wrong:3909): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed (wrong:3909): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed
发生这种情况时,该行中的一些图像是空的(白色)。
当没有匹配的符号(图像)放置在行上时输出:
[chim] ~/source/matrix > ./wrong Symbol[1] [images/L1.jpg] Symbol[6] [images/H3.jpg] Symbol[3] [images/L3.jpg] Symbol[0] [images/LO.jpg] Symbol[4] [images/H1.jpg]
在这种情况下,所有图像都会正确显示。
关于如何修复以及我可能做错了什么的任何建议?