You are not logged in.

#26 2012-06-12 22:11:06

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] Can you tell me why this segfaults?

Awesome, right documentation page, just wrong part of it.  See my edit above.

Exploring those doc pages should help polish this project off.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#27 2012-06-12 22:15:41

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] Can you tell me why this segfaults?

Trilby wrote:

Awesome, right documentation page, just wrong part of it.  See my edit above.

Exploring those doc pages should help polish this project off.

Okay, as I said I will look into this tomorrow. smile


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#28 2012-06-13 14:27:44

jdarnold
Member
From: Medford MA USA
Registered: 2009-12-15
Posts: 485
Website

Re: [SOLVED] Can you tell me why this segfaults?

I really recommend the free ebook "C++: A Dialog" (it used to be called "Who's Afraid of C++?"). You can find it here:

http://www.steveheller.com/cppad/cppad.htm

It really goes from beginner to expert, as he discusses programming with a complete newbie.

Offline

#29 2012-06-13 14:55:12

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] Can you tell me why this segfaults?

jdarnold wrote:

I really recommend the free ebook "C++: A Dialog" (it used to be called "Who's Afraid of C++?"). You can find it here:

http://www.steveheller.com/cppad/cppad.htm

It really goes from beginner to expert, as he discusses programming with a complete newbie.

Thanks! But.. is C++ alot different than C?


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#30 2012-06-13 17:35:02

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] Can you tell me why this segfaults?

Unia wrote:

Okay, as I said I will look into this tomorrow. smile

I am doing so at the moment but still can't get it to work mad Trilby, the code you suggested, e.g.

struct mpd_status *mystatus = mpd_recv_status(conn);

doesn't change a thing. Do I have to add something else? I already tried lots of things and am also spitting through those document pages I found, but yea... can't get it to work. Here's what I have atm:

...
struct mpd_status *mystatus = mpd_recv_status(conn);
if (mystatus == MPD_STATE_PLAY) {
            mpd_response_next(conn);

            song = mpd_recv_song(conn);

            if ((title = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_TITLE, 0), -1)) == NULL)
                title = TEXT_UNKNOWN;
            if ((artist = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -1)) == NULL)
                artist = TEXT_UNKNOWN;
            if ((album = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), -1)) == NULL)
                album = TEXT_UNKNOWN;

            notification = (char *) malloc(strlen(TEXT_PLAY) + strlen(title) + strlen(artist) + strlen(album));
            sprintf(notification, TEXT_PLAY, title, artist, album);

            mpd_song_free(song);
}
else if (mystatus == MPD_STATE_PAUSE) {
            notification = (char *) malloc(strlen(TEXT_PAUSE));
            sprintf(notification, TEXT_PAUSE);
}
        else {
            notification = (char *) malloc(strlen(TEXT_STOP));
            sprintf(notification, TEXT_STOP);
        }
...

If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#31 2012-06-13 21:57:46

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] Can you tell me why this segfaults?

Unia wrote:
...
struct mpd_status *mystatus = mpd_recv_status(conn);
if (mystatus == MPD_STATE_PLAY) {
            mpd_response_next(conn);
...

You are comparing a state to a status.  You need to compare states to states.  You get the state using mpd_status_get_state and passing in the status ... Only after you know the status is valid.

I refer you to a quote from my previous post:

 struct mpd_status* theStatus = mpd_recv_status(conn);
 if ( ! theStatus)
   /*Don't use a null pointer.  You may want a more graceful error handler */
  exit(1);
if (mpd_status_get_state(theStatus) == MPD_STATE_PLAY)
{

Edit: Fixed two minor code issues.  It is a struct, not a typedef.

Last edited by ewaller (2012-06-13 21:59:55)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#32 2012-06-15 13:56:14

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] Can you tell me why this segfaults?

Sorry for not replying yesterday. I got to hear I passed my finals at High School so I was busy celebrating with friends and family smile (In case you wonder now, I'm 17 years old)

Today I fiddled around with the code a tad and suddenly got it working! I don't know how 'efficient' it is but at least it works!

In case anyone wants to use it too (or review the code wink) here it is:
https://gist.github.com/2936589


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#33 2012-06-15 14:34:12

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] Can you tell me why this segfaults?

That is a whole lot more complex than the code I was writing when I was your age.  Well Done.

And, far more importantly, congratulations on the exams.

(Yikes, you are my daughter's age yikes )


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#34 2012-06-15 14:34:47

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] Can you tell me why this segfaults?

ewaller wrote:

That is a whole lot more complex than the code I was writing when I was your age.  Well Done.

And, far more importantly, congratulations on the exams.

(Yikes, you are my daughter's age yikes )

Thanks! And eh... got an email? big_smile big_smile (joking, of course wink )


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

Board footer

Powered by FluxBB