0

Hello I have a memory issue in my iPad app. Each time I change from a view to another view (this transition is made with segues), the app is increasing the memory used and never releases the memory. It is always increasing the memory used.

Let's see an example:

I am in my first view "home" which has these lines in viewDidLoad and viewDidAppear

(void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self initializeHomeDataSources];
    DateService* dateService = [[DateService alloc] init];
    self.currentDate = [dateService today];

    [self checkHomeStatus];
    [self showEmptyHomeViews];
    [self setUpFonts];
}

and this my view did appear method

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];    
    _homeAutomaticUpdate = YES;
    //This is a Thread
    [NSThread detachNewThreadSelector:@selector(automaticHome) toTarget:self withObject:nil];
    [self.phrasesView startPhrasesThread];

    if ([InternetService internetConnection]) {
        [self synchronizeHome];
    }

    if (self.scheduleDataSource.currentEvent) {
        [self loadMessagesFor:self.homeDataSource.currentEvent];
        [self loadLibraryFor:self.homeDataSource.currentEvent];
    } else {
        [self loadLibrary];
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    _homeAutomaticUpdate = NO;
}

All the IBOutlet's are defined as (nonatomic, strong).

Each time the HomeView is loaded the memory increases it's quantity and I don't know what is happening.

Can anybody help me here? This problem is causing me consternation.

Dean
  • 939
  • 10
  • 30
Gabriel Goncalves
  • 5,132
  • 3
  • 25
  • 34
  • ARC?, no ARC?, is dealloc ever called? have you tried instruments? – Grady Player Nov 15 '13 at 15:32
  • Doesn't it detach a new thread every time the view does appear? What are you trying to accomplish? – Fr4ncis Nov 15 '13 at 15:34
  • Have you tried using the analyzer or memory leak tools to track down the problem? – ThomasW Nov 15 '13 at 15:40
  • Fr4ncis I'm detaching a new thread every time the view appear because I need to refresh automatically a view – Gabriel Goncalves Nov 15 '13 at 15:49
  • I think that the problem is not the Thread. Because the memory is increasing 2MB each time I load the view – Gabriel Goncalves Nov 15 '13 at 15:51
  • You can use instruments to find out *why* the memory is increasing by 2MB each time. I recommend using Activity Monitor over Allocations, but try both and see if that helps you find the culprit. – Guy Kogus Nov 15 '13 at 15:53
  • Hey I already check the instruments and I can see a memory leak, how can I find where it happends? the instruments show me that some Classes have a memory leak but How can I check where it is happening? – Gabriel Goncalves Nov 15 '13 at 15:59

4 Answers4

0

Few questions:

  1. Is your app killed after a while, because of memory usage?
  2. Why you are creating new thread in -viewDidAppear?
  3. Have you tried to simulate memory warning? (In simulator: Hardware -> Simulate Memory Warning or Shift + CMD + M)

Does the memory gets down after memory warning or not?

Vytautas
  • 573
  • 3
  • 8
  • I have never see a memory warning in the app but I think that It could happen because the memory is increasing 2MB each time the View is opened – Gabriel Goncalves Nov 15 '13 at 15:47
0

This is not a whole answer for your question but your outlets must be weak unless their not top level objects.

Boran
  • 949
  • 10
  • 17
  • It's completely depends on a situation. Apple just recommends to use ``weak``, because there are few situation when ``strong`` is required. So, there is no **must**, there is only **should**. – Vytautas Nov 15 '13 at 15:58
0

All the IBOutlet's used should be (nonatomic, weak). Try this out..

dRAGONAIR
  • 1,181
  • 6
  • 8
0

I'm guessing that you're going "backwards" to previous controllers using segues. Is that true? If so, that's your problem -- unless you use an unwind segue, you should never go backwards using segues because they always instantiate new controllers. So, when going back to previous controllers, either use an unwind, or use code that reverses your forward segues -- that is, popViewControllerAnimated: if the forward segue was a push, and dismissViewControllerAnimated:completion: if the segue was a modal.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Well It is not always true but the main menu redirect a view controller always using segues. But the user can go backwards through the navigation bar. How can I use the unwind segue? can you show me an example? – Gabriel Goncalves Nov 15 '13 at 16:17
  • @Gabox, look at my answer here: http://stackoverflow.com/questions/16158586/app-running-slow-after-loading-viewcontroller-then-unload-about-15-20-times/16160239#16160239 – rdelmar Nov 15 '13 at 16:22
  • @Gabox, also check out Apple's "Using Unwind Segues" document which you can find by typing "unwind" into the Documentation and API Reference under the help menu in Xcode. – rdelmar Nov 15 '13 at 16:26
  • I read the documentation about Unwind Segues, but I don't understand how It's going to help me with the menu. The Main Menu is UITableViewController where each cell has a segue to each view controller in my app. Now I realize that those viewController never get deallocated, but I don't understant how can I prevent that. Because the user can go back using the Main Menu which appears in all the viewControllers – Gabriel Goncalves Nov 15 '13 at 16:55
  • @Gabox, what do you mean by "can go back using the Main Menu"? Is there a main menu button in each of these controllers? If so, what code is that button executing? – rdelmar Nov 15 '13 at 16:58
  • Each View Controller has a lateral menu (on the left part). This lateral menu is a table view, and each cell has a segue to a View Controller. I reuse that lateral menu in all my view controllers. – Gabriel Goncalves Nov 15 '13 at 17:03
  • This Main Menu have a prepareForSegue method which pass a object to each View Controller – Gabriel Goncalves Nov 15 '13 at 17:05
  • @Gabox, i'm not sure I understand your app structure, but if you're going back from a controller to a previous controller, the controller you're leaving from will be deallocated if you go back using an unwind segue, popToViewController, or dismissViewController. You need to use one of those methods when going backwards, not a push or modal segue. – rdelmar Nov 15 '13 at 17:07
  • Ok I understand. I have that problem I never call dissmissViewController or popViewController. Can I call one of those methods in prepareForSegue? – Gabriel Goncalves Nov 15 '13 at 17:27
  • When the navigation Item is showed and you go back using it, is the source view controller popped? – Gabriel Goncalves Nov 15 '13 at 17:30
  • @Gabox, if you mean the back button on the navigation bar, then yes, the controller you are going back from should be deallocated. – rdelmar Nov 15 '13 at 18:16