0

I have a strange problem with a method is called each time a button is pressed:

- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
  MyObject* obj = ...

  MyViewController* detailView = [[MyViewController alloc] init];  // line A
  [self.navigationController pushViewController:detailView animated:YES];
  [detailView setObject:obj];
  detailView = nil;  // should I also release it? -- line B
}

The problem is that I apologize I have to release detailView (memory tool shows me I have a memory leak is it is not done), also because navigationController should retain my detailView, but both if I try to add autorelease in line "A" or in line "B", or simply a release for detailView in line "B" (of course before assigning it nil), the program crashes with an EXC_BAD_ACCESS 'cause release message sent to deallocated instance [CALayer]...

Any idea? Thanks a lot

Dmitri Sologoubenko
  • 2,909
  • 1
  • 23
  • 27

4 Answers4

2

try it this way

- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
  MyObject* obj = ...

  MyViewController* detailView = [[MyViewController alloc] init]; 
  [detailView setObject:obj];
  [self.navigationController pushViewController:detailView animated:YES];
  [detailView release];
  detailView = nil;  // now this will be optional
}
Vaibhav Tekam
  • 2,344
  • 3
  • 18
  • 27
  • No way, it crashes the same way. I also made an attempt as follows: transformed "object" in a retain property and implemented a new "refreshView" method which takes the retained instance of "object" and set detailView labels accordingly, next moved call to [detailView setObject:obj] before pushView (as you suggested) and then inserted a [detailView refreshView] immediately after pushViewController call, but the crash persists. I have to set detailViews IBOutlets AFTER pushViewController because (I apologize due to memory usage optimization) if I call it before they will be all nil! – Dmitri Sologoubenko Mar 25 '11 at 17:08
1

Does this work without crashing?

- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
  MyObject* obj = ...

  MyViewController* detailView = [[MyViewController alloc] init];
  [self.navigationController pushViewController:detailView animated:YES];
  //[detailView setObject:obj];  // <- What's this for?
  [detailView release]
}
Kirby Todd
  • 11,254
  • 3
  • 32
  • 60
  • [detailView setObject:obj]; sets MyViewController's view label captions accondingly to MyObject properties. I have learned that I have to display my NIB-based view BEFORE attempting to set labels & Co., otherwise my IBOutlet label properties in controller are found as nil by setObject methods, and so setObject become useless... Isn't it true? – Dmitri Sologoubenko Mar 25 '11 at 16:39
  • I've made an attempt as you suggested, and it does NOT crash... So, the crash seems to depend upon [detailView setObject:obj] call! But i HAVE to set my IBOutlet's properties AFTER pushing view controller, otherwise all IBOutlets will be nil, don't I? My current implementation on setObject in MyViewController releases any previously retained obj, then retains obj and then sets IBOutlet's properties (captions) accordingly. Ah, "obj" is a ManagedObject from Core Data. – Dmitri Sologoubenko Mar 25 '11 at 17:18
  • MY MISTAKE, SORRY for timeloss! :) Thank you for your answer: I've found an improper release of an object in my setObject method. THANK YOU VERY MUCH and sorry again. – Dmitri Sologoubenko Mar 25 '11 at 17:49
0

try "initwithnibname"

unrelated but if you chase memory leaks don't forget to release MyObject

Bogdan S
  • 1
  • 1
-1

When you set detailView = nil; without releasing it, you only nil the pointer to the memory. The block of memory is still allocated until you release it.

You must use [detailView release] before detailView = nil or else you will have no way to reference that block of memory again (memory leak).

  • Sorry, as I already said I think I should release it to avoid memory leaks, but I've tried and the app crashes with "EXC_BAD_ACCESS 'cause release message sent to deallocated instance [CALayer]", this is the real problem! – Dmitri Sologoubenko Mar 25 '11 at 16:43