Is it a memory leak?

I have the following 2 code snippets. Suppose I have a parent class, and in the Parent.h class I have

@property (retain) NSMutableArray *childrens; 

and I synthesized it correctly in the .m file. suppose in the file Parent.m

- (void) dealloc

{

  [childrens release]; [super dealloc]; 

}

In another class, I declare the following:

1.

 Parent *p = [[Parent alloc] init]; p.chidlrens = [[NSMutableArray alloc] init]; // do some other stuff 

2.

 Parent *p = [[Parent alloc] init]; NSMutableArray *childArray = [[NSMutableArray alloc] init]; p.childrens = childArray; [childArray release]; 

From the above 2 methods, is there a leak in method 1?

+4
source share
4 answers

Yes, there is a leak in method 1. You assign an NSMutableArray, but do not release it.

+5
source

Without answering your question, I would recommend that Parent init allocate an array of the Init method in it, so your code in another place does not need to worry about checking its creation and execution. Then you do not need to have classes that use Parent to manage parent memory. Parent can do this.

+2
source

the general rule is to release everything you do, alloc, init on.

but build + analyze to get some "potential" leaks. if you want to be 100% sure, run it though leaks

+1
source

To check where the leak is, you can without the help of anyone. For example, use any plugins for this. I use deleaker and know exactly where the memory leak is.

+1
source

All Articles