指针立即0x0

发布于 2024-10-04 23:50:42 字数 1529 浏览 2 评论 0原文

我有这个代码。指针在使用前立即变为 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 开始计数,get_column() 从 0 开始计数。

i have this code. the pointer turns 0x0 immediately before using it. short before, it had the correct address.

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

i use Gtkmm 2.4, but it returns the expected value, it just turns 0x0. whats wrong?

gdb proof:

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 ()

i have no idea what causes this phenomenon. do you have?

Solution:
reading the documentation. the function returning pcFolder counts from 1, get_column() from 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆光下的微笑 2024-10-11 23:50:42

函数调用:

preview->get_column(pcFolder);

返回NULL。

当 gdb 显示当前代码行时,直到您键入下一个代码,它才被执行。

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

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

The function call:

preview->get_column(pcFolder);

returns NULL.

When gdb shows the current code line, it hasn't been executed until you type next.

You probably pass an index that is larger than the number of columns in preview. Try:

p pcFolder
p preview->get_columns().size()
再见回来 2024-10-11 23:50:42

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

preview->get_column(); returns NULL, before that, its just some random value, since you didn't initialize the col variable

讽刺将军 2024-10-11 23:50:42

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

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

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

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

Better code would actually be to initialise the variable immediately on use by calling getColumn at the point of declaration:

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

If this function can return NULL (as it appears to) you must then check before you use the pointer, thus:

if( col != NULL )
{
   col->set_resizable( true );
}
// else handle the "error" if you want
清醇 2024-10-11 23:50:42
preview->get_column(pcFolder)

必须返回 0。

preview->get_column(pcFolder)

must be returning 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文