c++ - 以 root 身份运行时出现段错误?

标签 c++ linux gdb maemo

当我从我的计算机上以 root 身份运行时,我的 C++ 程序给我一个段错误,但当我启动远程 session 时却没有。我的程序仅作为用户从我的计算机运行。可能是什么问题?我为嵌入式设备编写了程序,并使用它进行编译:

gcc -Werror notify.cc -o notify `pkg-config --libs --cflags gtk+-2.0 hildon-notifymm hildonmm hildon-fmmm'

我没有收到任何错误。会不会是标志问题?我可以发布我的代码。

编辑:当我用 gdb 启动我的程序时,我得到了这个:

Program received signal SIGSEGV, Segmentation fault.
0x40eed060 in strcmp () from /lib/libc.so.6
0x40eed060 <strcmp+0>:  ldrb    r2, [r0], #1

回溯给出这个:

(gdb) backtrace
 #0  0x40eed060 in strcmp () from /lib/libc.so.6
 #1  0x40b7f190 in dbus_set_g_error ()
 from /usr/lib/libdbus-glib-1.so.2
 #2  0x40b7d060 in dbus_g_bus_get () from /usr/lib/libdbus-glib-1.so.2
 #3  0x400558ec in notify_init () from /usr/lib/libnotify.so.1
 #4  0x4004a240 in Notify::init(Glib::ustring const&) ()
 from /usr/lib/libnotifymm-1.0.so.7
 #5  0x40033794 in Hildon::notify_init(Glib::ustring const&) ()
 from /usr/lib/libhildon-notifymm-1.0.so.1

这是我的代码:

#include <hildonmm.h>
#include <hildon-notifymm.h>
#include <hildon/hildon-notification.h>
#include <libnotifymm/init.h>
#include <gtkmm/stock.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <iostream>

int main(int argc, char *argv[])
{
// Initialize gtkmm and maemomm:

Hildon::init();
Hildon::notify_init("Notification Example");

// Initialize D-Bus (needed by hildon-notify):
DBusConnection* conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
dbus_connection_setup_with_g_main(conn, NULL);

// Create a new notification:
Glib::RefPtr<Hildon::Notification> notification =   Hildon::Notification::create("Something Happened", "A thing has just happened.", Gtk::Stock::OPEN);

// Show the notification:
std::auto_ptr<Glib::Error> ex;
notification->show(ex);
if(ex.get())
{ 
std::cerr << "Notification::show() failed: " << ex->what() << std::endl;
}
return 0;
}

编辑:问题已解决。程序需要终端环境中的 DBUS_SESSION_ADDRESS。

最佳答案

问题是您在某处调用了未定义的行为。未定义的行为在不同的机器上可能表现不同,在同一台机器上运行不同,等等。您必须找到让野指针发生的位置并处理它。

很可能你只是在以受限用户身份运行时获得“幸运”,或者你进程的页面权限设置为允许你获得的任何无效内存访问,或者你有一些特定于 root 的代码' 仅在用户模式下运行时达到。

关于c++ - 以 root 身份运行时出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6233656/

相关文章:

android - 有没有办法在 C++ 中以编程方式执行 adb 命令?这部分 C++ 代码是使用 android studio 中的 ndk build 构建的。那里

c++ - 存储类列表并通过在 C++ 中进行子类化来专门化函数?

c++ - 针对 C++20 契约(断言)进行测试

linux - GPFS : mmremote: Unable to determine the local node identity

android - 如何使用 GDBSERVER 在 Android 上调试应用程序?

c++ - 在 gdb 中调用 operator<<

c++ - 重用分配的内存空间

linux - 安装 expat XML 解析器以在 Arm 板上使用蓝牙

c - 非阻塞网络地址解析(gethostbyname 或 getaddrinfo)?

c - 模拟缓冲区溢出时返回地址错误