【问题标题】:libspotify crashes with segFault after calling sp_session_createlibspotify 在调用 sp_session_create 后因 segFault 崩溃
【发布时间】:2013-08-28 15:03:04
【问题描述】:

我正在测试 libspotify 库(Linux 版本 12.1.51 x86),当我调用 sp_session_create() 时出现分段错误,应用程序不断崩溃。

我还没有应用程序密钥,也没有高级 Spotify 帐户,但这不应该是崩溃的原因,因为如果我没记错的话,有一个无效应用程序密钥的错误代码。

我的代码如下:

static uint_8_t g_appkey[] = {1, 2, 3};
static const char *username = "MyUsername";
static const char *password = "MyPassword";
static int logged_in;

static sp_session_callbacks session_callbacks;
static sp_session_config spconfig;

static void on_login(sp_session *session, sp_error error) {
    printf("Callback: on_login");
    if (error != SP_ERROR_OK) {
        printf("Error: Unable to login: %d\n", (int) error);
        exit(-1);
    }
    logged_in = 1;
}

static void on_main_thread_notified(sp_session *session) {
    printf("callback: on_main_thread_notified");
}

static void on_log_message(sp_session *session, const char *data) {
    printf("callback: on_log_message");
}

int main(int argc, char **argv) {
    sp_error error;
    sp_session *session;
    int next_timeout;

    /* struct fill */
    memset(&session_callbacks, 0, sizeof(session_callbacks));
    memset(&spconfig, 0, sizeof(spconfig)); 

    session_callbacks.logged_in          = &on_login;
    session_callbacks.notify_main_thread = &on_main_thread_notified;
    session_callbacks.log_message        = &on_log_message; 

    spconfig.api_version          = SPOTIFY_API_VERSION;
    spconfig.cache_location       = "tmp";
    spconfig.settings_location    = "tmp";
    spconfig.application_key      = g_appkey;
    spconfig.application_key_size = sizeof(g_appkey);
    spconfig.user_agent           = "spot";
    spconfig.callbacks            = &session_callbacks;

    /* session creation */  
    error = sp_session_create(&spconfig, &session);
    if (error != SP_ERROR_OK) {
        printf("ERROR: Unable to create spotify session: %s\n", sp_error_message(error));
        exit(-1);
    }

    /* log in */
    logged_in = 0;
    sp_session_login(session, username, password, 0, NULL);
    while(!logged_in) {
        sp_session_process_events(session, &next_timeout);
        sleep(next_timeout);
    }

    printf("Sucess!!");
    exit(0);
}

关于问题出在哪里的任何提示?

感谢您提供的任何帮助。


来自 gdb 的回溯:

[Thread debugging using libthread_db enabled]
[New Thread 0xb7fe6b70 (LWP 1839)]
[New Thread 0xb7f65b70 (LWP 1840)]

Program received signal SIGSEGV, Segmentation fault.
0x002b9b36 in sp_session_create () from /usr/local/lib/libspotify.so.12
(gdb) thread apply all backtrace

Thread 3 (Thread 0xb7f65b70 (LWP 1840)):
#0  0x0012d422 in __kernel_vsyscall ()
#1  0x003e6ce6 in nanosleep () at ../sysdeps/unix/syscall-template.S:82
#2  0x0041644c in usleep (useconds=10000) at ../sysdeps/unix/sysv/linux/usleep.c:33
#3  0x00293581 in ?? () from /usr/local/lib/libspotify.so.12
#4  0x00293990 in ?? () from /usr/local/lib/libspotify.so.12
#5  0x001d42b7 in ?? () from /usr/local/lib/libspotify.so.12
#6  0x004ae96e in start_thread (arg=0xb7f65b70) at pthread_create.c:300
#7  0x0041ca4e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130

Thread 2 (Thread 0xb7fe6b70 (LWP 1839)):
#0  0x0012d422 in __kernel_vsyscall ()
#1  0x004b5245 in sem_wait@@GLIBC_2.1 () at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/sem_wait.S:80
#2  0x002178fa in ?? () from /usr/local/lib/libspotify.so.12
#3  0x001d42b7 in ?? () from /usr/local/lib/libspotify.so.12
#4  0x004ae96e in start_thread (arg=0xb7fe6b70) at pthread_create.c:300
#5  0x0041ca4e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130

Thread 1 (Thread 0xb7fe78d0 (LWP 1836)):
#0  0x002b9b36 in sp_session_create () from /usr/local/lib/libspotify.so.12
#1  0x080487d5 in main ()
(gdb) 

【问题讨论】:

  • 请提供 gdb 回溯/堆栈跟踪 - 使用 gdb。即使您没有有效的用户/密码登录,它也不应该崩溃,而是返回一个有效的值,指示登录失败。

标签: c spotify libspotify


【解决方案1】:

问题解决了。

我从 spotify 获得了一个有效的应用程序密钥,测试了我的代码,现在它可以工作了。

当前的 libspotify 版本在输入无效的应用程序密钥时似乎存在错误。

【讨论】:

    【解决方案2】:

    存储类为static的变量不需要被置空,默认情况下它们是

    /* struct fill */
    memset(&session_callbacks, 0, sizeof(session_callbacks));
    memset(&spconfig, 0, sizeof(spconfig));
    

    确保这些文件夹确实存在

    编辑:实际上cache_location 应该由库创建。

    spconfig.cache_location       = "tmp";
    spconfig.settings_location    = "tmp";
    

    完整示例见:

    http://damienradtke.org/playing-with-the-spotify-api/

    【讨论】:

    • 我咨询了那个网站。从文件夹开始,它就存在。
    • 尝试不同的文件夹名称并检查它是否被创建,为其父目录声明执行用户的wr权限。
    • 所有检查,还是一样。即使把 x 权限,仍然不工作。已经重新下载了 libspotify.dll 还是一样。
    • 应该是libspotify.so*.dll是windows foo
    • 我的错,我的意思是.so。
    【解决方案3】:

    您的假应用密钥非常短。查看一个有效的应用程序密钥,它有 321 个字节长,前两个字节是大端数 322。我猜可能前两个字节告诉 libspotify 它需要分配多大的空终止字符串来存储整个钥匙。如果 libspotify 信任它而不是 application_key_size,这可能就是它崩溃而不是返回错误的原因。

    【讨论】:

    • 试过了,同样的错误。做了类似uint16_t *ptr = &g_appkey[0]; *ptr = htobe16(322); 的东西并改变了数组的大小。
    【解决方案4】:

    我使用此代码并针对 libspotify-12 构建它,并让它执行时出现有关应用 ID 的预期错误:

    libspotify/examples/jukebox$ make
    cc -I/usr/include/alsa   -I/home/nik/Code/spotify/libspotify/targets/Linux-x86_64-release/include -Wall   -Wl,-rpath,/home/nik/Code/spotify/libspotify/targets/Linux-x86_64-release/lib -L/home/nik/Code/spotify/libspotify/targets/Linux-x86_64-release/lib jukebox.o appkey.o alsa-audio.o audio.o -o jukebox -lasound   -lpthread -lspotify
    libspotify/examples/jukebox$ ./jukebox
    ERROR: Unable to create spotify session: Invalid application key
    

    如果您在启动和运行时遇到问题,我建议您查看 libspotify 附带的示例代码,特别是 jukebox 示例。在上面的 shell 示例中,我只是将 jukebox.c 替换为您的代码,并且可以毫无问题地构建它。

    这里可能存在一些错误,该错误已在更高版本的 libspotify 中得到修复(免责声明:我为 Spotify 工作,实际上用最新的 12.x 代码编译了上面的示例,其中可能包含一些未发布的错误修正)。但是,代码本身似乎并没有做任何不寻常的事情,但同样,如果您遇到问题,我建议您调整 jukebox.c 以适应您的目的。

    【讨论】:

    • 我尝试运行点唱机示例,但它一直在崩溃。甚至在另一台 linux 机器上进行了测试,结果相同。我所做的唯一更改是在 appkey.c 文件中使用我的假应用程序密钥。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2013-09-26
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多