Sunday, December 8, 2013

Memory leak problem

The following code
headerViewArray = [[NSMutableArray alloc] init];
UIView * customView;

customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];

Dealloc function in there:
[HeaderViewArray release];

Q: The above customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)]; whether or not a memory leak
If you need to call
[CustomView release];
That is, into the following:
headerViewArray = [[NSMutableArray alloc] init];
UIView * customView;

customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
[CustomView release];
customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
[CustomView release];
customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
[CustomView release];
customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
[CustomView release];
It does not produce memory leaks
Reply:
Need to call
[CustomView release];
Reply:
You can:
customView = [[[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)] autorelease];

You can also:
customView = [[UIView alloc] initWithFrame: CGRectMake (0,0,300,40)];
[HeaderViewArray addObject: customView];
[CustomView release];

No comments:

Post a Comment