Strings Prematurely Deallocated(or I Think)

Discussion in 'iOS Programming' started by ahan.tm, Feb 5, 2014.

  1. ahan.tm macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    Hi,

    So, I'm working on an app in which I have an array that contains 5000 custom objects. Each of those custom objects contains 5 strings, a bool, and some ints. I use a loop to populate this data upon app load. If I add a NSLog right after I populate, the strings contain what they're supposed to. When I present the data on a table view, many of the strings are now null. The strings are actually populated while the table view is already on screen, with a loading icon. When I say "many of the strings", some of them are fine, and this only affects a certain string in each object out of the 5. The app worked perfectly fine a week ago, and I already reverted to my last git commit, but issue still appears. Is this a bug with iOS?

    Thanks,
    ahan.tm
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    It's quite impossible to debug this without seeing any of the involved code.

    I find it interesting that your git revert didn't fix your issue, too. Maybe you should share with us some of the git history (IE, did you do anything with your repo that would have altered its history? Possibly you have some untracked files?)
     
  3. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    The array will retain all objects so I wouldn't worry about that one.

    Possible issue is the populating of your object, maybe some objects aren't populated at initialization. I don't know how you are doing this. But you say a NSLog after would suggest it's all there.

    What you are left with then is the populating of each cell. You can start by logging the object that is going to populate the cell. This is in cellForRowAtIndexPath. That will prove to you that the data is actually there.

    What I'm thinking is that when you re-use the cell something is going wrong. And post some code too! :)
     
  4. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #4
    Hey guys,

    Thanks for your input. So, today, I open the app in Xcode and run it in the simulator, and guess what? It works! I really don't know how, as I did not change anything. I checked the stats, and I'm only using around 90 mb of memory.

    Was this possibly a memory bug with iOS 7? If it was, then it should be filed with :apple:.
     
  5. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #5
    Okay. Now about that last post. . .

    So, like 5 minutes after posting that post. I delete the app from the sim, and rebuild, and rerun. I changed a little bit of code.

    Now, most of the names are there, but not all. I revert my code back, reset sim, run, and the same issue is there.

    I'm seriously thinking that this is an iOS bug. I might request Technical Assistance from Apple if that's the case.

    Here is my code for reference. I have removed important URL's and changed them to SOME URL.

    Code:
     NSCharacterSet *newlineCharSet = [NSCharacterSet newlineCharacterSet];
        NSString *fileContents = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"SOME URL"]
                                                          encoding:NSUTF8StringEncoding
                                                             error:nil];
        
        NSArray *lines = [fileContents componentsSeparatedByCharactersInSet:newlineCharSet];
        
        
        for (int i = 0; i < [lines count]-1; i++)
        {
            NSString *key = lines[i];
            
            NSArray *keyItems = [key componentsSeparatedByString:@"|"];
            Chart *newChart = [Chart alloc];
            newChart.code = keyItems[0];
            newChart.state = keyItems[1];
            
            newChart.chartURL = [NSURL URLWithString:[NSString stringWithFormat:@"SOME URL", newChart.code]];
            
            
            
    
            if (keyItems.count == 4)
            {
                newChart.coordinate = CLLocationCoordinate2DMake([keyItems[2] doubleValue], [keyItems[3] doubleValue]);
                newChart.private = NO;
            }
            else
            {
                newChart.private = YES;
                
            }
            
            [self.allLocations addObject:newChart];
    
        }
        
        newlineCharSet = [NSCharacterSet newlineCharacterSet];
        fileContents = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"SOME URL"]
                                                          encoding:NSASCIIStringEncoding
                                                             error:nil];
        
        lines = [fileContents componentsSeparatedByCharactersInSet:newlineCharSet];
        
        for (int i = 0; i < [lines count]-1; i++)
        {
            NSString *key = lines[i];
            NSArray *keyItems = [key componentsSeparatedByString:@"|"];
            
            NSPredicate *codePredicate = [NSPredicate predicateWithFormat:@"%K = %@", @"code", keyItems[0]];
            
            NSArray *predicateArr = [self.allLocations filteredArrayUsingPredicate:codePredicate];
            
            int index = [self.allLocations indexOfObject:predicateArr[0]];
            
            ((Chart *)[self.allLocations objectAtIndex:index]).name = keyItems[2];
    
        }
    
        [self saveAllLocations]; //Saves using NSCoder, this is NOT the problem. 
        
        
    }
    
    Thanks,
    ahan.tm
     
  6. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #6
    Have you tried adding some logging where you populate the cell.
    I'm looking for a bit of proof that the NSArray have deallocated the objects. I'm pretty certain that's not the case you see...
     
  7. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #7
    Yeah - I added a NSLog when I populate the cell. It returns (null) for many of the strings.
     
  8. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #8
    Ok, so in that case, how are you saving and retrieving these strings?

    Try to just read back the whole lot after saving it and see if it is still there.
     
  9. ElectricSheep macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #9
    Something that may be useful as you try to track down when these strings are being de-allocated is to associate an instance of a small logging object to each string using the Associated Objects feature of the Objective-C runtime. There is a bit of a writeup on this technique here.

    Associated Objects allow you to extend an existing class member variables at runtime, similarly to how you can create categories to extend the behavior of existing classes. By associated an object with your strings, you can attach code that will be executed as soon as the string is deallocated. This should make it much more apparent when and where the deallocation is taking place.
     
  10. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #10
    And again, it now works. I haven't noticed any issues in the past 2 days with it. I'm looking into using Associated Objects for further testing. I'll keep y'all in the loop.

    Thanks,
    ahan.tm
     
  11. chown33, Feb 8, 2014
    Last edited: Feb 9, 2014

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    Show the exact NSLog in the code that populates the cell.

    Also post an example of its output when logging nulls, and tell us what each logged null should be instead. In short, describe exactly what you expected to happen (the specific strings), and show the evidence of what actually happened.


    Post the code for the Cell Chart class, both its @interface and its @implementation.

    If the strings are null before being added to a Cell Chart instance, then they will remain that way. You say they're not null, but we need to see your exact code. This is why I asked for you to show the NSLog code, to see where it gets the strings to log.

    If the strings are not null when added do a Cell Chart instance, but become so later, then logic suggests the problem lies in Cell Chart, which is the owner of the strings it holds. This is why I asked for Cell's Chart's code; to see how it handles its own strings. If a Cell Chart fails to retain or copy a string given to it, then the string can disappear at an unpredictable future time.


    Finally, you say that you're running the code in the simulator. The simulator isn't iOS, it's OS X.
     

Share This Page