Friday, 15 January 2010

c - mutex_init() causes weird segfault -


I am stopping at a strange issue that is trying to deal with posix threads. I'll start with the code:

  #include & lt; Pthread.h & gt; # Include & lt; Semaphore.h & gt; Type-Ff Structure {pthread_mutex_t * mutex; } Buffer_t; Buffer_t * buffer_loc (unsigned integer maximum size) {buffer_t * buffer = (buffer _t *) mlok (size (buffer_t)); If refund (buffer == faucet); Pthread_mutex_init (buffer-> mutex, NULL); // This line causes crash pthread_mutex_t * mutex; Pthread_mutex_init (mute x, tap); // This is not a   

}

I have a partition error for the first time at pthread_mutex_init () . Here's the GNB's Runlogo and Backatas:

  [Thread debugging enabled using libthread_db] using the host LibTrade_db library "/lib/x86_64-linux-gnu/libthread_db.so.1" is. Program received signal SIGSEGV, Segmentation fault. __pthread_mutex_init (mutex = 0x0, mutexattr = 0x0) at Pthread_mutex_init.c: 83 83 pthread_mutex_init.c: There is no such file or directory (Gdb) on backtrace # 0 pthread_mutex_init (mutex = 0x0, mutexattr = 0x0) pthread_mutex_init.c: 83 # 1 0x000000004015a8 in buffers_alloc (maxSize = 10) at buffers.c: 26   

Thanks for your help!

The indicator that you are passing through pthread_mutex_init () is unpublished, allocated memory , So your program invokes undefined behavior what you can do to modify your structure or buffer_loc () function to explicitly allocate memory for Mute X . I. E., Solution # 1 (I like this one):

  typed format {pthread_mutex_t mutex; } Buffer_t; Buffer_t * buffer_loc (unsigned integer maximum size) {buffer_t * buffer = molk (size (* buffer)); If refund (buffer == faucet); Pthread_mutex_init (and buffer-> mutes, tap); // return buffer etc; }   

Resolution # 1, dynamic allocation:

  typed format {pthread_mutex_t * mutex; } Buffer_t; Buffer_t * buffer_loc (unsigned integer maximum size) {buffer_t * buffer = molk (size (* buffer)); If refund (buffer == faucet); Buffer-> Mute x = mauloq (size (* (buffer-> mutes))); If (buffer-> mute x == faucet) {free (buffer); Return tap; } Pthread_mutex_init (buffer-> mutex, NULL); // return buffer etc; }   

In the latter case, do not forget about free with Mute X as well as "district".

In addition to this,

No comments:

Post a Comment