将内存分配给已经分配的变量?
我想为已经分配的变量分配内存。例如,
self.m_tabbarController = [[TabbarController alloc] init];
我必须更改上面的选项卡控制器分配的视图控制器。所以我必须释放 上面的内容并使用新控制器分配相同的选项卡栏。我怎样才能释放和分配 新的。如果我执行以下操作,则会崩溃。
if(self.m_tabbarController != nil)
{
[self.m_tabbarController release];
}
self.m_tabbarController = [[TabbarController alloc] init];
但自变量必须在 dealloc 方法中释放。请问有什么帮助吗?如果我也喜欢下面的内容,它会崩溃吗?
m_tabbarController = [[TabbarController alloc] init];
[self.window addSubview:m_tabbarController ];
[m_tabbarController release];
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.
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先释放它。假设您的合成属性是
retain
,实现将为您处理release
:简而言之,您不能依赖某种方式来重新初始化实例,除非您实现某种重新初始化类中的方法。
如果这是一个 UIViewController,只需创建一个新的 UIViewController,因为您需要了解有关实现、所有子类和所有成员/ivars 的大量信息才能正确实现重新初始化。有时您可以通过其公共属性来完成此操作,有时您将无法正确地重新初始化实例。
重新初始化的一个问题是您分配的内容可能不是返回的内容,而且您可能不知道在所有情况下您具体处理的类型。复杂类型的正确、详尽的重新实现会增加大量实现(这也往往是可传递的)。
First release it. Assuming your synthesised property is
retain
, the implementation will handle therelease
for you:In short, you cannot rely on some way to reinitialize an instance unless you implement some kind of reinitialization method in the class.
If that's a
UIViewController
, just create a newUIViewController
because you would need to know a lot about an implementation, all subclasses, and all members/ivars to implement reinitialization correctly. Sometimes you can accomplish this via its public properties, sometimes you won;t be able to reinitialize an instance correctly.One problem with reinitialization is that what you
alloc
may not be what's returned -- and you may not otherwise know what type you are dealing with specifically in all cases. Proper, exhaustive reimplementation of a complex type adds a lot of implementation (which tends to be transitive too).您应该声明带有保留类型属性的
m_tabbarController
。现在,修改您的代码如下:
另外,将
m_tabbarController
释放到dealloc
方法中。You should declare
m_tabbarController
with retain kind of property.Now, modify your code as below:
Also, release
m_tabbarController
intodealloc
method.[self.m_tabbarController release];
将释放m_tabbarController
而不是self
所以绝对没问题
[self.m_tabbarController release];
will releasem_tabbarController
notself
sois absolutely fine