1 
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 
7 /* Delete memory that has a held lock and see what happens. */
8 
9 typedef struct { int stuff[2000];
10                  pthread_mutex_t lock; int morestuff[2000]; } XX;
11 
12 void bar ( void );
13 void foo ( void );
14 
main(void)15 int main ( void )
16 {
17    XX* xx = malloc(sizeof(XX));
18    assert(xx);
19 
20    pthread_mutex_init( &xx->lock, NULL );
21 
22    pthread_mutex_lock( &xx->lock );
23 
24    free(xx);
25 
26    bar();
27    foo();
28    bar();
29 
30    return 0;
31 }
32 
33 /* Try the same, on the stack */
bar(void)34 void bar ( void )
35 {
36   pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
37   //  pthread_mutex_init( &mx, NULL );
38   pthread_mutex_lock( &mx );
39   /* now just abandon mx */
40 }
41 
42 /* and again ... */
foo(void)43 void foo ( void )
44 {
45   pthread_mutex_t mx;
46   pthread_mutex_init( &mx, NULL );
47   pthread_mutex_lock( &mx );
48   /* now just abandon mx */
49 }
50 
51