![]() ![]() "This was the release where zones were introduced, new was replaced with alloc/init, and stack based objects were no longer allowed." Michael Rutman told me (in 2009) that this happened in NeXTSTEP version 2.0 back in 1990. ![]() To go into a little bit of history, it was not always the case that stack based objects were forbidden in Objective-C. But this is a pretty advanced (and very difficult) piece of analysis it's much simpler to just all have objects allocated on the heap by default. Some compilers can perform a special trick known as escape analysis, where they analyze the object's lifetime and if they can prove that the object will not outlive its function, they rewrite the code that creates it to place it on the stack, making creation and cleanup cheaper. (Also consider the case of a function whose return value is a newly-created object, for example.) In all such cases, you need to keep the value in a non-ephemeral portion of memory. This is just one of many scenarios where you would need to keep an object around after the function that first created it returns. ![]() The main reason is that just because you're done with a local reference to an object doesn't mean you're finished with the object itself.įor example, if your function creates an object and adds it to a pre-existing collection that belongs to a broader scope, then the object has to be on the heap anything on the stack gets trashed after the function returns, but the collection (and all the objects it contains) is still valid. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |