简体   繁体   English

在ARC中进行保留计数

[英]make retain count in ARC

I am using an external library in my project which is being build in an ARC environment. 我在正在ARC环境中构建的项目中使用外部库。 As per the library the socket object gets deallocated only when the retain count=0. 根据库,仅当保留计数= 0时,套接字对象才被释放。 As far as I know its not liable to use retain count in ARC but I am forced to remove all the reference of the socket object which is not possible in my project. 据我所知,它不应该在ARC中使用保留计数,但是我被迫删除了套接字对象的所有引用,这在我的项目中是不可能的。 How can I resolve this issue? 我该如何解决这个问题? A gist of code issue is below: 代码要点如下:

-(void)callConnect{
   for(int i = 0; i<[userArray count];i++){
     [self connect:(NSString*)[userArray objectAtIndex:i]];
   }
}
-(void)connect:(NSString *)username{
    RTMPCLient *socket = [[RTMPClient alloc] init];
    BroadCastClient *stream = [[BroadCastClient alloc] initWithClient:socket];
    NSMutableDictionary *stream = [NSMutableDictionary dictionaryWithObject:stream forKey:username];
}
-(void)disconnect{
    for(int i = 0; i<[userArray count];i++){
      [stream objectForKey:[NSString stringWithFormat:@"%@",[userArray objectAtIndex:i]]] = nil; //error on this line
    BroadCastClient *tempStream = [stream objectForKey:[userArray objectAtIndex:i]];
    tempStream = nil;
   }  
}

I am trying to make the stream object nil which gives an error. 我试图使流对象nil给出错误。 Cannot save it another variable as it increases the references of socket object.By making the tempStream nil doesn't affect the original instance created. 无法保存另一个变量,因为它增加了套接字对象的引用。通过使tempStream为nil不会影响创建的原始实例。 I want to remove the reference of socket object from stream in the disconnect method. 我想从断开连接方法中的流中删除套接字对象的引用。 How can I do so? 我该怎么办?

IN ARC, you have to just make the objects to nil to maintain RC. 在ARC中,您只需将对象设置为零即可维护RC。 So you can do it in the following way. 因此,您可以按照以下方式进行操作。

-(void)disconnect{
  socket = nil;
  stream = nil;
  stream = nil;
}

-(void)connect:(NSString *)username{ 
   if (socket != nil )
      socket = nil;
   RTMPCLient *socket = [[RTMPClient alloc] init];

   if (stream != nil ) 
       stream = nil;
   BroadCastClient *stream = [[BroadCastClient alloc] initWithClient:socket];

   NSMutableDictionary *stream = [NSMutableDictionary dictionaryWithObject:stream forKey:username]; // Make it using alloc...then you must use nil only
}

ARC will put the invisible release message in your code (in connect), but the array will have strong reference on them, so they will stay in memory. ARC将不可见的release消息放入您的代码中(在连接中),但是该array将对它们具有强大的引用,因此它们将保留在内存中。 All you have to do in disconnect remove all the objects from your collection ( [stream removeAllObjects] and [userArray removeAllObjects] ) and the collection will release them. disconnect需要做的所有事情从集合中删除所有对象( [stream removeAllObjects][userArray removeAllObjects] ),集合将释放它们。

UPDATE: 更新:
By following your code I see the following: 通过遵循您的代码,我看到以下内容:
In this code you are creating an instance of BroadCastClient and adding it to NSDictionnary ( stream ), but NSDictionary has no reference to it, so it will be deallocated after the method call 在此代码中,您将创建BroadCastClient的实例并将其添加到NSDictionnarystream ),但是NSDictionary没有对其的引用,因此在方法调用之后将对其进行释放。

-(void)callConnect{
   for(int i = 0; i<[userArray count];i++){
     [self connect:(NSString*)[userArray objectAtIndex:i]];
   }
}
-(void)connect:(NSString *)username{
    RTMPCLient *socket = [[RTMPClient alloc] init];
    BroadCastClient *stream = [[BroadCastClient alloc] initWithClient:socket];
    NSMutableDictionary *stream = [NSMutableDictionary dictionaryWithObject:stream forKey:username];
}

Now here the disconnect stream dictionary (I don't know what is this object, because in your code I don't see any creating or adding to it) the object BroadCastClient is retained by the dictionary, so just removing this object from the dictionary will free it from memory (assuming you have no other strong reference to it) 现在在这里, disconnect stream字典(我不知道这个对象是什么,因为在您的代码中我看不到任何创建或添加的对象)对象BroadCastClient被字典保留,因此只需从字典中删除该对象将其从内存中释放出来(假设您没有其他强烈的参考)

-(void)disconnect{
    for(int i = 0; i<[userArray count];i++){
      [stream objectForKey:[NSString stringWithFormat:@"%@",[userArray objectAtIndex:i]]] = nil; //error on this line
    BroadCastClient *tempStream = [stream objectForKey:[userArray objectAtIndex:i]];
    tempStream = nil;
   }  
}

I would recommend some refactoring for your code, but before that please have some time to read this guid: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/Articles/mmPractical.html 我建议对您的代码进行一些重构,但是在此之前,请花一些时间阅读此指南: https : //developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/Articles/mmPractical.html

It looks like stream is an instance variable of type NSMutableDictionary * . 看起来streamNSMutableDictionary *类型的实例变量。 So if you want to remove the references in your stream dictionary, you could do it like this: 因此,如果您想删除stream字典中的引用,则可以这样做:

- (void)disconnect {
    for (int i = 0; i<[userArray count]; i++) {
      [stream removeObjectForKey:[userArray objectAtIndex:i]];
   }  
}

// Alternative version using Fast Enumeration:

- (void)disconnect {
    for (id key in userArray) {
        [stream removeObjectForKey:key];
    }
}

But if all you want to do is remove all references from stream , simply do: 但是,如果您要做的只是从stream删除所有引用,只需执行以下操作:

- (void)disconnect {
   [stream removeAllObjects];
}

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

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