简体   繁体   English

如何释放手动保留的类对象

[英]how to release a class object which is manually retained

I am working on a Tab Bar based application I have a class A with its .xib and a class B with its .xib 我正在开发一个基于Tab Bar的应用程序,我有一个带有.xib的A类和带有.xib的B类

On class A i am loading multiple instances of class B. 在A类中,我正在加载B类的多个实例。

ie In class A ,i am doing. 即在A级,我在做。

in .m file 在.m文件中

   -(void)renderData:(NSArray *)Data
    {
         for(int i=0;i<[Data count];i++)
            {
               B *grid=[[B alloc]initWithNibName:@"Beta" bundle:[NSBundle mainBundle]];
               .
               .
               . //do something with the grid object i.e assign new image etc..)
               [self.myGrid addObject:grid]; //i have a NSMutableArray myGrid declared in .h
               [grid release];  
            }
    }

now in the myGrid Array i have all the objects of the grid saved. 现在在myGrid数组中,我保存了网格的所有对象。

Now i am adding them to the class Aplha view. 现在我将它们添加到Aplha类视图中。

    for(int i=0;i<[myGrid count];i++)
      {
        B *grid1=[[myGrid objectAtIndex:i]retain]; //i have done this retain because when i try to come back to this tab or try to call the same function it crashes by saying message send to deallocated instance.
        [self.view addSubview:grid1.view];
       }

now my problem is that how to release the grid1 object that i have retained. 现在我的问题是如何释放我保留的grid1对象。

You are approaching this wrong. 你正在接近这个错误。 The problem here isn't how to release the grid1 object, it's why you are retaining them in the first place. 这里的问题不是如何释放grid1对象,这就是为什么你要保留它们的原因。 You most likely shouldn't be; 你很可能不应该; you need to investigate the original crash more thoroughly. 你需要更彻底地调查原始崩溃。

If your grid1 objects are stored in self.myGrid then they are retained by that array. 如果grid1对象存储在self.myGrid则它们将由该数组保留。 Are you releasing myGrid anywhere? 你在任何地方发布myGrid吗? As long as that is retained, your grid1 objects should be. 只要保留,您的grid1对象应该是。

In addition, there are some conceptual issues here. 此外,这里有一些概念性问题。 Loading a view controller from a nib and adding it's view as a sub-view of another view controller's view is generally not correct. 从笔尖加载视图控制器并将其视图添加为另一个视图控制器视图的子视图通常是不正确的。 It's hard to recommend the correct approach without knowing exactly what you are trying to achieve though. 如果不确切知道你想要达到的目的,很难推荐正确的方法。

You do not need to pass in [NSBundle mainBundle] to initWithNibName:bundle: - you can simply pass in nil as the default behaviour is to use the main bundle. 您不需要将[NSBundle mainBundle]传递给initWithNibName:bundle: - 您可以简单地传入nil,因为默认行为是使用主bundle。

Your comment says you have "assigned" an NSMutableArray in your header. 您的评论说您已在标题中“分配”了一个NSMutableArray You don't assign anything in your header, you just declare things. 您没有在标题中指定任何内容,只需声明内容即可。 Have you actually initialised the NSMutableArray somewhere in your implementation? 您是否在实现中的某处初始化了NSMutableArray

  • You own any object you create when 你拥有你创建的任何对象

You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy). 使用名称以“alloc”,“new”,“copy”或“mutableCopy”开头的方法(例如,alloc,newObject或mutableCopy)创建对象。

  • When you no longer need it, you must relinquish ownership of an object you own 当您不再需要它时,您必须放弃您拥有的对象的所有权

You relinquish ownership of an object by sending it a release message or an autorelease message. 您通过向对象发送释放消息或自动释放消息来放弃对象的所有权。 In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object. 因此,在Cocoa术语中,放弃对象的所有权通常被称为“释放”对象。

To release grid1 use [grid1 release]; 要释放grid1,请使用[grid1 release];


Note: There is no reason to retain/release your grid1 object in cycle. 注意:没有理由在循环中保留/释放grid1对象。 Just 只是

B *grid1 = (B*)[myGrid objectAtIndex:i];
[self.view addSubview:grid1.view];

I don't know what happened with your rest code but it looks like you have some memory leaks in another place. 我不知道你的休息代码发生了什么,但看起来你在另一个地方有一些内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM