gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:24 26 4
gpt4 key购买 nike

当我从我的计算机上以 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com