Re: cmd-q versus menu item quit
Re: cmd-q versus menu item quit
- Subject: Re: cmd-q versus menu item quit
- From: "b.bum" <email@hidden>
- Date: Tue, 11 May 2004 18:07:45 -0700
On May 11, 2004, at 2:41 PM, Nick Zitzmann wrote:
On May 11, 2004, at 2:39 PM, Milton Sagen wrote:
The simplest explanation is to say that I have a temporary file that
an object in the window deletes when its dealloc'd. The object is
dealloc'd when the user quits with the cmd-q but not when quitting
via the menu item, hence in the former case the file is removed but
not in the latter.
Stiphane is correct; in general -dealloc methods should just release
any member objects and disconnect itself from the rest of the
application (if it observes notifications, etc.). Instead of doing
what you're doing, you ought to make your object(s) listen for
NSApplicationWillTerminateNotification and then do the cleanup in the
observer for that notification.
If you have a temporary file that is only used by your application, you
can use unlink() to remove the file from the filesystem while still
retaining access to the file within your application. As soon as your
application terminates, the file will be really removed from the
filesystem such that the space used by the file becomes free again.
It is sort of a delete-the-file-as-soon-as-the-last-app-closes-it
paradigm.
From the man page:
#include <unistd.h>
int unlink(const char *path);
The unlink() function removes the link named by path from its
directory
and decrements the link count of the file which was referenced by
the
link. If that decrement reduces the link count of the file to
zero, and
no process has the file open, then all resources associated with
the file
are reclaimed. If one or more process have the file open when the
last
link is removed, the link is removed, but the removal of the file
is
delayed until all references to it have been closed.
This also has the advantage of causing the file to be deleted from the
file system and the resources reclaimed in the case where your
application terminates abnormally due to circumstances beyond your
control (hard reboot, power loss, force quit, etc...).
As long as you write the file into /tmp/, it will be reclaimed upon
reboot with two caveats that you should be aware of:
- if the temporary file contains any sensitive/confidential/private
data, then storing it in /tmp/ is a potential vulnerability.
Regardless of access permissions, an attacker can generally reboot the
machine into single user mode and read anything in the local filesystem
at will.
- if your app is left running for 3 days without touching the file, the
system will remove the file as part of the periodic cleanup scripts
(see /etc/daily).
b.bum
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.