Objective-c and memory leaks after program termination?

I am a new Objective-C programmer, coming from C #, VB.NET, etc. These are all garbage-collected languages, and in most cases the worst thing you can do is abuse the memory, because when your program shuts down, the memory returns at runtime.

However, I do not know about Objective-C. I get this, for the most part, for us as a developer to manage the distribution, initialization, retention and release of objects. I try my best to do this and slowly think that I am getting stuck on this.

My concern is this: I'm not sure if I understood the term "memory leak" correctly. Does this mean that you are freeing up memory incorrectly, and then when my application shuts down, unused memory is not used? In other words, when my program shuts down, does Mac OS not guarantee that everything that the program used is cleared?

I hope this makes sense, and in fact he understands the differences after the program shuts down, and not so much about the memory while the program is running.

+4
source share
3 answers

I am not sure if I understood the term "memory leak" correctly. Does this mean that you are freeing up memory incorrectly, and then when my application shuts down, unused memory is not used?

No; a memory leak is a memory leak while the application is still running, since OS X recovers all memory when the application terminates. If this were not so, it could cause huge problems due to poor coding and memory management and would affect the whole system.

It is mainly about saving an object too much or not freeing up enough memory. For example, when adding objects to an array, this is the usual way to do this:

NSMutableArray *array; //Pointer to some already allocated array [array addObject:someObject]; [someObject release]; 

Assuming that someObject been correctly allocated, it will start with a hold amount of 1. When you add objects to the array, the array calls retain on the object in question, pressing it in this case, 2.

When an array is released, it sends release all of its objects; in this case, the hold counter will be reset to 1 (provided that no one else has saved the object). It is not 0, so someObject still exists. If someObject was a local pointer created inside the method, and you no longer had a pointer to this object, then there would be memory there. This is an example of a memory leak and leads to the fact that your application uses more memory than necessary and will suffer until it is completed.

+3
source

The OS frees up memory, but before that you can leak. Therefore, if you receive a message about leaks on shutdown, these are just objects that have not yet been released (but the memory is now fixed).

I wrote a blog post about understanding objective-c memory management for iPhone.

http://loufranco.com/blog/files/managing-memory-iphone.html

and this tool in CLang static analysis, which I found invaluable for detecting leaks, exploring the source

http://loufranco.com/blog/files/scan-build-iphone.html

+1
source

No, the OS should free all memory when the application closes. A memory leak will crash your application if it is not processed. To make it simple to say, devices have 10 MB of RAM, and you constantly lose memory with every function call. Sooner or later, your ram will end and your application will crash.

Apple provides an excellent Tools tool that tracks and corrects memory leaks. I suggest you run the application through this from time to time to check for leaks. I went so far as to always find a place where I am going to release an object when I call alloc. If I call alloc and this is not autorelease, then I have to find a place to post the release.

0
source

Source: https://habr.com/ru/post/1286378/


All Articles