Memory still occupied after removeallobjects and release NSMutableArray

Discussion in 'iOS Programming' started by wli061, Oct 13, 2008.

  1. wli061 macrumors newbie

    Joined:
    Oct 13, 2008
    #1
    Hi Guys,
    I am trying to read in a file to a NSString and then creat a NSMutableArray to store the contents divided by "\n". However, after release the array, it looks like the memory still not released. The memory was increasing to 20MB after alloc the array and was still occupied after releasing the array. The memory of NSString can be released no problem. No memory leaks.
    Any help will be appreciated.
    HTML:
    /////////////////////
    	//read in test.txt
    	NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.txt"];
    	NSError *error;
    	
    	NSString *testtest = [[NSString alloc] initWithContentsOfFile:defaultDBPath encoding:NSASCIIStringEncoding error:&error];
    	NSMutableArray *Rna = [[NSMutableArray alloc] initWithArray: [testtest componentsSeparatedByString:@"\n"]];
    	[testtest release];
    	/////////////////////
    	//remove all temp arrays
    	[Rna removeAllObjects];
    	[Rna release];
    
     
  2. SqueegyX macrumors regular

    Joined:
    Mar 24, 2008
    #2
    I'm not sure why you are leaking, but you shouldn't need to remove all objects before you release. When an NSArray is dealloc'd, it releases all of the objects in contains automatically.
     
  3. wli061 thread starter macrumors newbie

    Joined:
    Oct 13, 2008
    #3
    Hi SqueegyX, there was no leaks. I tried both autoreleased and released arrays. Looks like they were still occupied the memory after release. Any ideas? I wonder this is a bug on iphone simulator and will try the program on device later.
     
  4. detz macrumors 65816

    Joined:
    Jun 29, 2007
    #4
    Is it because garbage collection? That memory is set to be released but the GC has not actually released it yet....
     
  5. wli061 thread starter macrumors newbie

    Joined:
    Oct 13, 2008
    #5
    It has to be managed memory on iphone. I tried on device and simulator. The autoreleased arrays still took the memory. Is there a way to find out when the autoreleasing happens? Thanks guys.
     
  6. wli061 thread starter macrumors newbie

    Joined:
    Oct 13, 2008
    #6
    I have tried on device. It was same result. The reason should be that the autorelease array was not released yet. I even tried aurorelease pool, but no luck. Here are more codes:
    1. In app Delegate, I initial a MyViewController.
    HTML:
    - (void)applicationDidFinishLaunching:(UIApplication *)application {	
    	
    	// Override point for customization after app launch	
        MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@"ControllerView" bundle:[NSBundle mainBundle]];
    	self.myViewController = aViewController;
    	[aViewController release];
    	UIView *controllersView = [myViewController view];
    	[window addSubview:controllersView];
    	[window makeKeyAndVisible];
    }
    - (void)dealloc {
    	[myViewController release];
    	[window release];
    	[super dealloc];
    }
    
    2. In MyViewController, I have a function linked to a button.
    HTML:
    - (IBAction)RnaRns:(id)sender
    {
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"rna.crs"];
    	NSError *error;
    	NSString *testtest = [[NSString alloc] initWithContentsOfFile:defaultDBPath encoding:NSASCIIStringEncoding error:&error];
    	NSArray *Rnaarray = [[testtest componentsSeparatedByString:@"\n"] autorelease];
    	[testtest release];
    }
    
    I wondered the Rnaarray could be only released when the MyViewController was released. I did try put - (IBAction)RnaRns:(id)sende in an object, then initial it in MyViewController and use the function . Then released the object. However, the Rnaarray was still not released. So the real question is how we make sure the autorelease is released and when. Autoreleasepool is not working for this case as well. I cannot afford losing these memory on iphone. Thanks.
     

Share This Page