Re: preventing bad memory access
Re: preventing bad memory access
- Subject: Re: preventing bad memory access
- From: Don Quixote de la Mancha <email@hidden>
- Date: Thu, 20 Oct 2011 20:56:34 -0700
On Thu, Oct 20, 2011 at 8:48 PM, Jens Alfke <email@hidden> wrote:
> What’s unsafe is accessing file-mapped memory after the file becomes
> unavailable.
> If you use fread (or read), you’re not doing that. You're allocating your
> own memory from the heap first, then copying data from the file into it.
> There’s nothing dangerous about that. The memory will be available until you
> explicitly free it.
All the "f" Standard I/O calls return result codes. The calls will
fail if your I/O fails for some reason, for example a disk being
ejected after you've opened it but before you've had a chance to read
it, or if you attempt to open a file only to find it missing.
While Mac OS X discourages the user from dismounting filesystems that
have open files on them, the user can forcibly dismount them. If
nothing else, they can pull out a USB key, or disconnect a FireWire
cable or ethernet cable over which one is getting data from a file
server.
Use "man fopen", "man fread", "man fwrite", "man fseek" and "man
fclose" to see what the error codes are.
To be robust in the face of I/O error, you will need to design your
application well. This is not a trivial task for a novice. It takes
some experience to learn how to do it.
For C++, you would need to throw an exception on I/O error, then write
exception-safe code. I have not yet really figured out the right way
to do the equivalent for Objective-C, because Objective-C exceptions
don't work like C++ exceptions do.
You can pass result codes back to calling functions, but you have to
be quite diligent about always doing so, always checking for results
from the functions you call, then doing something sensible no matter
what results your functions or methods or member functions return.
--
Don Quixote de la Mancha
Dulcinea Technologies Corporation
Software of Elegance and Beauty
http://www.dulcineatech.com
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden