{"id":3454,"date":"2014-03-25T11:25:38","date_gmt":"2014-03-25T11:25:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/25\/libspotify-crashes-with-segfault-after-calling-sp_session_create-collection-of-common-programming-errors\/"},"modified":"2014-03-25T11:25:38","modified_gmt":"2014-03-25T11:25:38","slug":"libspotify-crashes-with-segfault-after-calling-sp_session_create-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/25\/libspotify-crashes-with-segfault-after-calling-sp_session_create-collection-of-common-programming-errors\/","title":{"rendered":"libspotify crashes with segFault after calling sp_session_create-Collection of common programming errors"},"content":{"rendered":"<p>I was testing out libspotify library (version 12.1.51 x86 for linux) and the application keeps crashing when I call sp_session_create() with a segmentation fault.<\/p>\n<p>I don&#8217;t have application key, nor a Premium Spotify account (yet), but that shouldn&#8217;t be the reason for the crash, since if I remember correctly, there is an error code for invalid application key.<\/p>\n<p>My code is as follows:<\/p>\n<pre><code>static uint_8_t g_appkey[] = {1, 2, 3};\nstatic const char *username = \"MyUsername\";\nstatic const char *password = \"MyPassword\";\nstatic int logged_in;\n\nstatic sp_session_callbacks session_callbacks;\nstatic sp_session_config spconfig;\n\nstatic void on_login(sp_session *session, sp_error error) {\n    printf(\"Callback: on_login\");\n    if (error != SP_ERROR_OK) {\n        printf(\"Error: Unable to login: %d\\n\", (int) error);\n        exit(-1);\n    }\n    logged_in = 1;\n}\n\nstatic void on_main_thread_notified(sp_session *session) {\n    printf(\"callback: on_main_thread_notified\");\n}\n\nstatic void on_log_message(sp_session *session, const char *data) {\n    printf(\"callback: on_log_message\");\n}\n\nint main(int argc, char **argv) {\n    sp_error error;\n    sp_session *session;\n    int next_timeout;\n\n    \/* struct fill *\/\n    memset(&amp;session_callbacks, 0, sizeof(session_callbacks));\n    memset(&amp;spconfig, 0, sizeof(spconfig)); \n\n    session_callbacks.logged_in          = &amp;on_login;\n    session_callbacks.notify_main_thread = &amp;on_main_thread_notified;\n    session_callbacks.log_message        = &amp;on_log_message; \n\n    spconfig.api_version          = SPOTIFY_API_VERSION;\n    spconfig.cache_location       = \"tmp\";\n    spconfig.settings_location    = \"tmp\";\n    spconfig.application_key      = g_appkey;\n    spconfig.application_key_size = sizeof(g_appkey);\n    spconfig.user_agent           = \"spot\";\n    spconfig.callbacks            = &amp;session_callbacks;\n\n    \/* session creation *\/  \n    error = sp_session_create(&amp;spconfig, &amp;session);\n    if (error != SP_ERROR_OK) {\n        printf(\"ERROR: Unable to create spotify session: %s\\n\", sp_error_message(error));\n        exit(-1);\n    }\n\n    \/* log in *\/\n    logged_in = 0;\n    sp_session_login(session, username, password, 0, NULL);\n    while(!logged_in) {\n        sp_session_process_events(session, &amp;next_timeout);\n        sleep(next_timeout);\n    }\n\n    printf(\"Sucess!!\");\n    exit(0);\n}\n<\/code><\/pre>\n<p>Any tips for where could be the problem?<\/p>\n<p>Appreciated for any help given.<\/p>\n<p>backtrace from gdb:<\/p>\n<pre><code>[Thread debugging using libthread_db enabled]\n[New Thread 0xb7fe6b70 (LWP 1839)]\n[New Thread 0xb7f65b70 (LWP 1840)]\n\nProgram received signal SIGSEGV, Segmentation fault.\n0x002b9b36 in sp_session_create () from \/usr\/local\/lib\/libspotify.so.12\n(gdb) thread apply all backtrace\n\nThread 3 (Thread 0xb7f65b70 (LWP 1840)):\n#0  0x0012d422 in __kernel_vsyscall ()\n#1  0x003e6ce6 in nanosleep () at ..\/sysdeps\/unix\/syscall-template.S:82\n#2  0x0041644c in usleep (useconds=10000) at ..\/sysdeps\/unix\/sysv\/linux\/usleep.c:33\n#3  0x00293581 in ?? () from \/usr\/local\/lib\/libspotify.so.12\n#4  0x00293990 in ?? () from \/usr\/local\/lib\/libspotify.so.12\n#5  0x001d42b7 in ?? () from \/usr\/local\/lib\/libspotify.so.12\n#6  0x004ae96e in start_thread (arg=0xb7f65b70) at pthread_create.c:300\n#7  0x0041ca4e in clone () at ..\/sysdeps\/unix\/sysv\/linux\/i386\/clone.S:130\n\nThread 2 (Thread 0xb7fe6b70 (LWP 1839)):\n#0  0x0012d422 in __kernel_vsyscall ()\n#1  0x004b5245 in sem_wait@@GLIBC_2.1 () at ..\/nptl\/sysdeps\/unix\/sysv\/linux\/i386\/i686\/..\/i486\/sem_wait.S:80\n#2  0x002178fa in ?? () from \/usr\/local\/lib\/libspotify.so.12\n#3  0x001d42b7 in ?? () from \/usr\/local\/lib\/libspotify.so.12\n#4  0x004ae96e in start_thread (arg=0xb7fe6b70) at pthread_create.c:300\n#5  0x0041ca4e in clone () at ..\/sysdeps\/unix\/sysv\/linux\/i386\/clone.S:130\n\nThread 1 (Thread 0xb7fe78d0 (LWP 1836)):\n#0  0x002b9b36 in sp_session_create () from \/usr\/local\/lib\/libspotify.so.12\n#1  0x080487d5 in main ()\n(gdb) \n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was testing out libspotify library (version 12.1.51 x86 for linux) and the application keeps crashing when I call sp_session_create() with a segmentation fault. I don&#8217;t have application key, nor a Premium Spotify account (yet), but that shouldn&#8217;t be the reason for the crash, since if I remember correctly, there is an error code for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3454","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3454"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3454\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}