title: What happens if pthread_key_delete is called on a key after a failed pthread_key_create?
body: <p>Suppose the following code:</p>
  
  <pre class="lang-c prettyprint-override"><code>pthread_key_t key;
  pthread_key_create(&amp;key, NULL);    /* failure here */
  pthread_key_delete(key);
  </code></pre>
  
  <p>If <code>pthread_key_create</code> fails, is the call to <code>pthread_key_delete</code> considered <em>undefined behavior</em>? How about if <code>pthread_key_create</code> is commented out?</p>
  
  <p>The <a href="http://pubs.opengroup.org/onlinepubs/009696799/functions/pthread_key_delete.html" rel="nofollow">pthread_key_delete</a> section of the POSIX standard states:</p>
  
  <blockquote>
    <p>The pthread_key_delete() function shall delete a thread-specific data key previously returned by pthread_key_create().</p>
  </blockquote>
  
  <p>Since <code>pthread_key_delete</code> expects a thread-specific data key <em>previously returned</em> by <code>pthread_key_create</code>, I'm afraid calling <code>pthread_key_delete</code> on a key that was not returned by <code>pthread_key_create</code> can lead to undefined behavior.</p>