开发者

Memory Leak warning while addSubView to Current ViewController

开发者 https://www.devze.com 2023-03-06 17:24 出处:网络
I get memory leak warning when I addsubView to Current ViewController....This is my code.. NoOfGolferViewController *objNoOfGolferViewController = [[NoOfGolferViewController alloc]initWithNibName:@\"

I get memory leak warning when I addsubView to Current ViewController....This is my code..

     NoOfGolferViewController *objNoOfGolferViewController = [[NoOfGolferViewController alloc]initWithNibName:@"NoOfGolferViewController" bundle:nil];
     [objNoOfGolferViewController setParent:self];
     [objNoOfGo开发者_开发百科lferViewController.view setFrame:CGRectMake(15, 110, 290, 330)];
     [self.view addSubview:objNoOfGolferViewController.view];

and when I release object

      [objNoOfGolferViewController release];

Application get crashed by giving EXE_BAD_ACCESS message.

How can I solve this memory leak warning?

Thanks in advance..


You need to keep your objNoOfGolferViewController object alive as long as its view is visible or used in current controller. The best solution it seems is to make it an instance variable of your current class and release objNoOfGolferViewController in its dealloc method


Probably you are declaring variable locally. Instead make it global and release it in dealloc. Reason behind that is when you are releasing the object ,delegate method releted to that are in progress .

OR

You can release the object after removing the view.


Typically you get this message when you are releasing an object that has been released already. I've been using the methods below (found here on SO at iOS4 - fast context switching) to track down these kind of issues in the past:

#pragma mark - RETAIN DEBUG MAGIC
// -----------------------------------------------------------------------------

- (id)retain
{
  NSLog(@"retain \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
  return [super retain];
}
- (void)release
{
  NSLog(@"release \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
  [super release];
}
- (id)autorelease
{
  NSLog(@"autorelease \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
  return [super autorelease];
}

I wrote about this a couple of days ago here at SO using a similar example (I was having issues retaining and releasing some views). Follow this link if you are interested: Understanding iOS Instruments

Good luck!


just make it autorelease ... like mentioned below

NoOfGolferViewController *objNoOfGolferViewController = [[[NoOfGolferViewController alloc]initWithNibName:@"NoOfGolferViewController" bundle:nil] autorelease];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号