Pwyll28
Pwyll28

Reputation: 3

UIViewcontroller never deallocated

I came here to ask for help because I'm on my way to finishing my iphone project and I want it to be perfect!

I have some issues with allocation and deallocation of a UIViewcontroller.

Let me explain :

I have a UITableview with custom cells. In each custom cells, a button that will allocate a new UIViewController : ListOfAGuestFriendViewController;

So, I've made a delegate which calls a method to make a "flip transition", and show my new ListOfAGuestFriendViewController view.

My problem is, and I've gotten the same problem with ALL of my addSubiew, that ListOfAGuestFriendViewController never get deallocated, or gets deallocated just after the view loads!

Can someone explain me exactly how I can make a perfect addSubview ?

Here's my code :

When i flip the view :

-(void)flipAView
{
    Guest *currentSelectedGuest = [self createAGuestUsingIndexPath:self.selectedIndex];

    ListOfAGuestFriendViewController *listOfAGuestFriendViewController = [[ListOfAGuestFriendViewController alloc]init];

    [listOfAGuestFriendViewController setCurrentGuestSelected:currentSelectedGuest];
    [listOfAGuestFriendViewController setMyListOfContactControllerDelegate:self];

    [UIView animateWithDuration:0.25 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight 
                           forView:self.tableviewContainerView cache:YES];
        [self.tableviewContainerView addSubview:listOfAGuestFriendViewController.view];
    }completion:^(BOOL finished){
        [listOfAGuestFriendViewController release];
        [self collapseCurrentExpandedCell];
    }];
}

When i want to go back :

-(IBAction)goBackButtonGotPressed:(id)sender{
    [UIView animateWithDuration:0.5 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft 
                           forView:[self.view superview] cache:YES];
        [self.view removeFromSuperview];
    }];
}

If I remove that line of code :

[listOfAGuestFriendViewController release];

My listOfAGuestFriendViewController never deallocates.

I don't use properties for this instanciation, but when i do, it's the same!

Upvotes: 0

Views: 1184

Answers (2)

Midhun MP
Midhun MP

Reputation: 107121

Let me explain with your code:

ListOfAGuestFriendViewController *listOfAGuestFriendViewController = [[ListOfAGuestFriendViewController alloc]init];

When you create an object the retain count will be 1.

When you do this: [self.tableviewContainerView addSubview:listOfAGuestFriendViewController.view]; the receiver view will retain the view. Then it's retaincount will become 2.

After this: [self.view removeFromSuperview]; retain count will become 1.

Every object will be deallocated only after it's retaincount becomes 0. In the above case it's 1, so it never call the dealloc method.

If you write this line: [listOfAGuestFriendViewController release]; after this [self.tableviewContainerView addSubview:listOfAGuestFriendViewController.view]; That means it's retain count is decremented from 2 to 1, so when you call the [self.view removeFromSuperview]; your view will be deallocated. Reference for addSubview Reference for Memory Management

Upvotes: 1

adriendenat
adriendenat

Reputation: 3475

Did you checked the dealloc function ? // Tu as une fonction dealloc dans tes ViewController qui est appelée automatiquement quand ta vue est retirée.

- (void)dealloc
{
    [yourThings release], yourThings = nil;
    [super dealloc];
}

Upvotes: 0

Related Questions