0

I want to allocate memory for the variable to which i have allocated already.for example

        self.m_tabbarController = [[TabbarController alloc] init];

I have to change assigned view controller for above tabbar controller.so i have to release the above and allocate the same tabbar with new controllers. how can I release and allocate new one.If i do the following, gives crashes in IOS5

  if(self.m_tabbarController != nil)
    {
      [self.m_tabbarController release];    
    }
             self.m_tabbarController = [[TabbarController alloc] init];

but self variable must be deallcated in dealloc method.any help please?if i do like following also, it gives crash?

   m_tabbarController = [[TabbarController alloc] init];
    [self.window addSubview:m_tabbarController ];
     [m_tabbarController release]; 
nameless
  • 809
  • 3
  • 9
  • 27

2 Answers2

0

Try this

in .h declare "my_tabBarController",

@property(nonatomic,retain)my_tabBarController;

in .m

@synthesize my_tabBarController;

If you want to assign new view controller, just make another instance of TabbarController and assingn to existing one.

 TabbarController *secondTabBar=[[TabbarController alloc]init];
 self.my_tabBarController=secondTabBar;
 [secondTabBar release];

and dont forget to release my_tabBarController in dealloc

rakeshNS
  • 4,227
  • 4
  • 28
  • 42
-1

For reallocating a @property which is of (retain) type, you must not release it explicitly

//this is wrong
if(self.m_tabbarController != nil)
    {
      [self.m_tabbarController release];    
    }
/////////////////////

only this line will work fine for reallocation.

    self.m_tabbarController = [[TabbarController alloc] init];

The reason is that -> for a (retain) property, when we do self.obj = nil or (new allocation), it means

[obj release];
obj = nil or (new allocation)

your app is crashing as m_tabbarController is facing a double release due to explicit and implicit release both. first time you are explicit calling release by [self.m_tabbarController release]; and next implicit release is happening at the time of assignment self.m_tabbarController = [[TabbarController alloc] init];

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • I have allocated already for "self.m_tabbarController ".how can i do again as self.m_tabbarController = [[TabbarController alloc] init]; – nameless Feb 09 '12 at 09:57
  • to backup my answer, here is the link: http://stackoverflow.com/questions/1210776/objective-c-difference-between-setting-nil-and-releasing , you should work on your basics little more to make them even stronger. Best of luck!! – Saurabh Passolia Feb 09 '12 at 10:06