Mehul Mistri
Mehul Mistri

Reputation: 15147

Memory Leak warning while addSubView to Current ViewController

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];
     [objNoOfGolferViewController.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..

Upvotes: 2

Views: 722

Answers (4)

Toncean Cosmin
Toncean Cosmin

Reputation: 535

just make it autorelease ... like mentioned below

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

Upvotes: 0

Edgar
Edgar

Reputation: 76

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!

Upvotes: 0

Rony
Rony

Reputation: 638

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.

Upvotes: 1

Vladimir
Vladimir

Reputation: 170849

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

Upvotes: 3

Related Questions