setting objects to nil.

Discussion in 'iOS Programming' started by larswik, Jun 16, 2014.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I have a game I am working on. As the user buys things UIImageViews are added to the view. Then the view is added to an NSMutableArray to store what objects have been added to the view. All of the UIImageViews are ivars.

    At the end of the round all the views get removed from the screen but the objects still exist, even though I set them to nil.

    Code:
        for (UIImageView __strong *userObject in objectsArray) {
            user2Gold += (userObject.tag == 200)? 3 : 0;
            [[self.view viewWithTag:userObject.tag]removeFromSuperview];
            userObject = nil;
        }
        [objectsArray removeAllObjects];
    
    Later in the program I do a simple test to check if the program is gone or is still hanging around.

    Code:
        if (spyView) {
            NSLog(@"SpyView is their");
    
        }
        else{
            NSLog(@"spyView is gone");
    
        }
    
    From my understanding I am creating a UIImageView pointer object called 'userObjects'. I am using this this to iterate through an array to remove the object from the view and also setting it to nil.

    At the end of the for loop I clean the array with removeAllObejcts. Now my gut is telling me something is wrong by the time I get to the array to clear it. I have already removed the objects from the view, and set them to nil. So the array would be holding pointer objects that pointers that should have been freed? I would think I would get some kind of an error, but I don't, and I don;t because the objects have not been set to nil.
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    Why aren't you just calling removeFromSuperview on the userObject itself, instead of searching through the self.view heirarchy by tag?

    Also, you can still call removeAllObjects on an empty array (and i believe an array that has nil pointers), so you shouldn't get an error there.

    Have you set breakpoints to see that the loop is actually running as you believe it is?

    Without more code I don't think we are going to be able to assist much more because we don't see how the array is populated and what sort of view structure the code is supporting
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    Thanks, I resolved it this morning. Instead of declaring ivars in the header I just create a UIImageView *newView as they are needed like so.

    Code:
    if (itemToPurchaseTag == 120) {
                userLandSpy = YES;
                UIImageView *spyView = [mrkt landSpy:location.x + 568 withYAxis:[[yAxisArrayForHill objectAtIndex:location.x + 568]integerValue]];
                [bg_view addSubview:spyView];
                [userDefenceObjects addObject:spyView];
            }
    
    
    Which called a method in a different class that returns the object.

    Code:
    -(UIImageView*)landSpy:(int)xLoc withYAxis:(int)yAxis{
        UIImage *guyImage = [UIImage imageNamed:@"spyImage"];
        UIImageView *spyView = [[UIImageView alloc]initWithImage:guyImage];
        CGRect guyRect = CGRectMake(xLoc, yAxis - 20, 8, 24); // need height because screenoriantation
        spyView.frame = guyRect;
        spyView.tag = 202;
        
        return spyView;
    }
    


    Ivars made it easy because I could just check to see if "spyView" was still a valid object. But now I just check for the tag number like so

    Code:
        if ([self.view viewWithTag: 202]) {
            NSLog(@"SpyView is their");
        }
        else{
            NSLog(@"spyView is gone");
        }
    
    I am guessing my original problem was that the pointer I created called "userObject" was pointing to the pointer "spyView" which was pointing to the location i'm memory where it was stored. So when I said "userObject = nil;", all I was doing was eliminating the "userObject pointer that was pointing to the "spyView" pointer, but "spyView" pointer was still a valid pointer pointing to the location in memory that held the data.
     

Share This Page