将内存分配给已经分配的变量?

发布于 2025-01-03 03:29:50 字数 629 浏览 3 评论 0原文

我想为已经分配的变量分配内存。例如,

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喜爱纠缠 2025-01-10 03:29:51

首先释放它。假设您的合成属性是 retain,实现将为您处理 release

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

简而言之,您不能依赖某种方式来重新初始化实例,除非您实现某种重新初始化类中的方法。

如果这是一个 UIViewController,只需创建一个新的 UIViewController,因为您需要了解有关实现、所有子类和所有成员/ivars 的大量信息才能正确实现重新初始化。有时您可以通过其公共属性来完成此操作,有时您将无法正确地重新初始化实例。

重新初始化的一个问题是您分配的内容可能不是返回的内容,而且您可能不知道在所有情况下您具体处理的类型。复杂类型的正确、详尽的重新实现会增加大量实现(这也往往是可传递的)。

First release it. Assuming your synthesised property is retain, the implementation will handle the release for you:

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

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 new UIViewController 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).

叹倦 2025-01-10 03:29:51
self.m_tabbarController = nil;
TabbarController *tempController = [[TabbarController alloc] init];
self.m_tabbarController = tempController;
[tempController release];
tempController = nil;
self.m_tabbarController = nil;
TabbarController *tempController = [[TabbarController alloc] init];
self.m_tabbarController = tempController;
[tempController release];
tempController = nil;
—━☆沉默づ 2025-01-10 03:29:51

您应该声明带有保留类型属性的m_tabbarController

现在,修改您的代码如下:

TabbarController *temp = [[TabbarController alloc] init];

self.m_tabbarController = temp;

[temp release];

另外,将 m_tabbarController 释放到 dealloc 方法中。

You should declare m_tabbarController with retain kind of property.

Now, modify your code as below:

TabbarController *temp = [[TabbarController alloc] init];

self.m_tabbarController = temp;

[temp release];

Also, release m_tabbarController into dealloc method.

梦回梦里 2025-01-10 03:29:51

[self.m_tabbarController release]; 将释放 m_tabbarController 而不是 self 所以

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

绝对没问题

[self.m_tabbarController release]; will release m_tabbarController not self so

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

is absolutely fine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文