Question

I am trying to stream audio for an application I am building but I have run into a snag with a library I am using. This library is libSndFile. The library always has a SEGFAULT whenever I call sf_open_fd(int fd, int mode, SF_INFO *info, int close_desc); I am using this function instead of sf_open(const char *path, int mode, SF_INFO *info) because I need to have libSndFile decode data that is in a buffer in memory. I couldn't make sense of the sf_virtual_open() documentation, so I am using this instead. I can get a valid file descriptor using fileno() (it returns 14 usually) and my FILE stream is valid, so it is just this function. I don't know where it might be SEGFAULTing or on what. Is there a better way for me to have libSndFile decode a memory buffer or am I not using something right?

Here is the problem code (I have added some comments to have it make more sense)

memset(&/*SF_INFO*/mInfo, 0, sizeof(SF_INFO));

/* FILE* */mpFileStream = tmpfile();

fwrite(/* void* */aBuffer, /*int*/aSize, 1, mpFileStream);

int iDescriptor = fileno(mpFileStream);

if(iDescriptor == -1)
{
    return /*enum EError*/EError_Unknown;
}

/* SNDFILE* */mFile = sf_open_fd(iDescriptor, SFM_READ, mInfo, SF_TRUE);

return mFile != NULL ? /*enum EError*/EError_None : /*enum EError*/EError_Unknown;

I really prefer to not be writing to the disk, but I had to to get a valid file descriptor and stream.

NOTE: I am compiling on Xubuntu 12.04 with g++ 4.6.3.

Was it helpful?

Solution

Probably on the first line:

memset(&/*SF_INFO*/mInfo, 0, sizeof(SF_INFO));

You're calling sf_open_fd() later using mInfo as a pointer parameter, so I'm guessing mInfo is actually a SF_INFO* rather than an SF_INFO. memset() allows any pointer type so you don't get a compile error.

Incidentally, you shouldn't need to write to the disk - tmpfile() returns an opened file that should work with fileno().

Edit: Actually rereading your question I may have misunderstood when you said you didn't know where the SEGFAULT is occurring. One thing I found in the libsndfile docs is that sf_open_fd() can't be used if your program is linked against a different runtime than the library. That might be the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top