title: Where do you put cleanup code for NSDocument sub-classes?
body: <p>I have a document-based application and I have sub-classed <code>NSDocument</code> and provided the required methods, but my document needs some extensive clean-up (needs to run external tasks etc). Where is the best place to put this? I have tried a few different methods such as:</p>
  
  <ul>
  <li><code>close</code></li>
  <li><code>close:</code></li>
  <li><code>canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo</code></li>
  <li><code>dealloc</code></li>
  </ul>
  
  <p>If I put it in <code>dealloc</code>, sometimes it gets called and other times it does not (pressing Command+Q seems to bypass my document's deallocation), but it is mandatory that this code gets called without failure (unless program unexpectedly terminates).</p>