Skip navigation.
 
mlRe: Create NSStrings from a mapped NSData object - safe?
FROM : Douglas Davidson
DATE : Tue May 13 17:53:16 2008

On May 13, 2008, at 8:08 AM, Jens Alfke wrote:

> While we're on the topic, it's worth noting that memory-mapping 
> files on removable or network filesystems can be dangerous. If you 
> read/write a mapped memory location, and the kernel has to page it 
> in, but the file's filesystem is no longer accessible due to a 
> network issue or a yanked USB cable ... you get a bus-error. I've 
> seen discussion of ways to handle this by installing a signal 
> handler whenever you access mapped memory, but it would be pretty 
> tricky to pull off. The conclusion was that it's only safe to memory-
> map files that are either (a) on the boot filesystem, or (b) in the 
> user's home directory. (The latter might be on a networked 
> filesystem, but if it ever gets disconnected, most of the upper 
> layers of the OS and applications will be hosed anyway...)


I would add, (c) files that are on the same volume as the executable; 
for example, files in your application bundle or one of your framework 
bundles should be safe to map in.  The reasoning here is that if a 
volume containing an executable your application is currently using 
goes away, the application is dead anyway--in fact, the executables 
themselves are usually mapped.  I'm not so certain about (b); it may 
be possible to survive the loss or temporary absence of the user's 
home directory, depending on what your application is doing.  However, 
the files I typically have wanted to map in have been fixed data files 
that are part of the system, the application, or one of its 
frameworks, for which the home directory would be an unlikely location 
anyway.

Douglas Davidson

Related mailsAuthorDate
mlCreate NSStrings from a mapped NSData object - safe? Daniel Vollmer May 13, 08:38
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 13, 09:15
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 13, 17:00
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 13, 17:08
mlRe: Create NSStrings from a mapped NSData object - safe? Douglas Davidson May 13, 17:53
mlRe: Create NSStrings from a mapped NSData object - safe? Daniel Vollmer May 13, 21:55
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 14, 01:39
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 05:57
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 06:11
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 06:39
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 14, 07:32
mlRe: Create NSStrings from a mapped NSData object - safe? Jens Alfke May 14, 07:33
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 08:28
mlRe: Create NSStrings from a mapped NSData object - safe? Michael Vannorsdel May 14, 10:26
mlRe: Create NSStrings from a mapped NSData object - safe? Mike Fischer May 14, 16:32
mlRe: Create NSStrings from a mapped NSData object - safe? Jean-Daniel Dupas May 14, 17:05
mlRe: Create NSStrings from a mapped NSData object - safe? Dennis Munsie May 15, 18:42
mlRe: Create NSStrings from a mapped NSData object - safe? Andrew Farmer May 16, 07:01