2010-11-30 85 views
2

我有这段代码。指针在使用前立即变为0x0。之前很短,它有正确的地址。指针即刻0x0

TreeViewColumn *col; 
col = preview->get_column(pcFolder); /* col = 0x7fff5fc404a0 */ 
col->set_resizable(true);   /* col = 0x0 */ 

我使用Gtkmm 2.4,但它返回期望值,它只是变成0x0。怎么了?

GDB证明:

151    col = preview->get_column(pcFolder); /* col = 0x7fff5fc404a0 */ 
(gdb) print col 
$1 = ('Gtk::TreeViewColumn' *) 0x7fff5fc404a0 
(gdb) print *col 
warning: can't find linker symbol for virtual table for `Gtk::TreeViewColumn' value 
$2 = { 
    <Gtk::Object> = { 
    <Glib::Object> = { 
     <Glib::ObjectBase> = <invalid address>, 
     members of Glib::Object: 
     _vptr$Object = 0x7fff5fc06a20, 
     static object_class_ = {<No data fields>} 
    }, 
    members of Gtk::Object: 
    static object_class_ = {<No data fields>}, 
    referenced_ = 21, 
    gobject_disposed_ = 60 
    }, 
    members of Gtk::TreeViewColumn: 
    static treeviewcolumn_class_ = {<No data fields>} 
} 
(gdb) next 
152    col->set_resizable(true);   /* col = 0x0 */ 
(gdb) print col 
$3 = ('Gtk::TreeViewColumn' *) 0x0 
(gdb) print *col 
Cannot access memory at address 0x0 
(gdb) next 

Program received signal EXC_BAD_ACCESS, Could not access memory. 
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000 
0x00000001000edc68 in Gtk::TreeViewColumn::set_resizable() 

我不知道是什么原因导致了这种现象。你有?

解决方案: 阅读文档。返回pcFolder功能从1计数,从0

+0

get_column是什么样的? – Nim 2010-11-30 10:51:37

+0

http://library.gnome.org/devel/gtkmm/2.22/classGtk_1_1TreeView.html#a513e50ad0acdd817d1a443f1eaa3debe – M3t0r 2010-11-30 10:57:21

回答

3

函数调用:

preview->get_column(pcFolder); 

返回null。

当gdb显示当前的代码行时,直到输入next后才会执行。

您可能传递的索引大于preview中的列数。尝试:

p pcFolder 
p preview->get_columns().size() 
1
preview->get_column(pcFolder) 

get_column()必须返回0。

2

preview->get_column();返回NULL,在此之前,它只是一些随机值,因为你没有初始化col可变

2

更好的代码实际上是立即初始化上使用的变量通过声明的时候调用getColumn:

TreeViewColumn *col = preview->get_column(pcFolder);

如果这个函数可以返回NULL(因为它似乎)您必须在使用指针之前进行检查,因此:

if(col != NULL) 
{ 
    col->set_resizable(true); 
} 
// else handle the "error" if you want