<legend id='IbSEo'><style id='IbSEo'><dir id='IbSEo'><q id='IbSEo'></q></dir></style></legend>
      <bdo id='IbSEo'></bdo><ul id='IbSEo'></ul>
    <i id='IbSEo'><tr id='IbSEo'><dt id='IbSEo'><q id='IbSEo'><span id='IbSEo'><b id='IbSEo'><form id='IbSEo'><ins id='IbSEo'></ins><ul id='IbSEo'></ul><sub id='IbSEo'></sub></form><legend id='IbSEo'></legend><bdo id='IbSEo'><pre id='IbSEo'><center id='IbSEo'></center></pre></bdo></b><th id='IbSEo'></th></span></q></dt></tr></i><div id='IbSEo'><tfoot id='IbSEo'></tfoot><dl id='IbSEo'><fieldset id='IbSEo'></fieldset></dl></div>

    1. <small id='IbSEo'></small><noframes id='IbSEo'>

      <tfoot id='IbSEo'></tfoot>

      何时在 NSURLConnection 委托上调用 release?

      时间:2023-09-12
        <bdo id='dW0u2'></bdo><ul id='dW0u2'></ul>

            <i id='dW0u2'><tr id='dW0u2'><dt id='dW0u2'><q id='dW0u2'><span id='dW0u2'><b id='dW0u2'><form id='dW0u2'><ins id='dW0u2'></ins><ul id='dW0u2'></ul><sub id='dW0u2'></sub></form><legend id='dW0u2'></legend><bdo id='dW0u2'><pre id='dW0u2'><center id='dW0u2'></center></pre></bdo></b><th id='dW0u2'></th></span></q></dt></tr></i><div id='dW0u2'><tfoot id='dW0u2'></tfoot><dl id='dW0u2'><fieldset id='dW0u2'></fieldset></dl></div>

              <tbody id='dW0u2'></tbody>
            <legend id='dW0u2'><style id='dW0u2'><dir id='dW0u2'><q id='dW0u2'></q></dir></style></legend>

            <small id='dW0u2'></small><noframes id='dW0u2'>

              • <tfoot id='dW0u2'></tfoot>
              • 本文介绍了何时在 NSURLConnection 委托上调用 release?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                将委托传递给 NSUrlConnection 对象时,如下所示:

                When passing a delegate to the a NSUrlConnection object like so:

                [[NSURLConnection alloc] initWithRequest:request delegate:handler];
                

                你什么时候应该在委托上调用 release?它应该在 connectionDidFinishLoading 中吗?如果是这样,我会不断收到 exec_bad_access.我看到我的代表正在通过仪器泄漏.

                when should you call release on the delegate? Should it be in connectionDidFinishLoading? If so, I keep getting exec_bad_access. I'm seeing that my delegates are leaking through instruments.

                谢谢

                推荐答案

                取自我的博文在这里:http://i.ndigo.com.br/2012/01/releasing-nsurlconnection-and-its-delegate/

                您必须特别注意委托对象,因为对于 NSURLConnection,委托有一个特殊考虑:它始终被保留.

                You will have to pay extra attention to the delegate object as, for NSURLConnection, there is a special consideration for the delegate: it is always retained.

                http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/doc/uid/20001697-BAJDDIDG

                initWithRequest:delegate:

                特别注意事项:连接保留代表.它释放委托时连接完成加载,失败或被取消.

                Special Considerations: The connection retains delegate. It releases delegate when the connection finishes loading, fails, or is canceled.

                因此,考虑到这一点,您有多种选择来确保您的委托将被正确释放,我将尝试解释 2 个简单的选择.

                So, taking that into consideration, you have several options to ensure that your delegate will be released correctly and I will try to explain 2 simple ones.

                第一个也是最常用的,是使用初始化 NSURLConnection 的同一个类作为委托.

                The first, and most commonly used, is to use the same class that initialize the NSURLConnection as the delegate.

                [[NSURLConnection alloc] initWithRequest:request self];
                

                通过这样做,您的类的保留计数将在连接开始时增加 1,然后在连接完成加载、失败或被取消后减少 1,从而不会导致内存泄漏.

                By doing that, your class the retain count would be increased by 1 when the connection starts and then would de reduced by 1 after the connection finishes loading, fails, or is canceled, resulting in no memory leaks.

                第二个选项,即您尝试做的那个,是使用另一个对象来处理所有连接调用.这也很好,但你需要特别注意记忆.解决问题的一个简单方法是使用自动释放对象初始化连接.

                The second option, the one that you are trying to do, is to use another object to handle all connection calls. This works fine as well, but you will need extra attention with memory. One simple thing you could do to solve your problem is to initialize the connection with an autorelease object.

                //creates the handler object
                MyHandlerClass *handler = [[MyHandlerClass alloc] init];
                
                //creates the connection with handler as an autorelease object
                [[NSURLConnection alloc] initWithRequest:request delegate:[handler autorelease]];
                

                或者您可以在创建连接后立即释放您的处理程序(因为它已被连接保留)

                OR you could release your handler right after creating the connection (as it will be already retained by the connection)

                //creates the handler object
                MyHandlerClass *handler = [[MyHandlerClass alloc] init];
                
                //creates the connection with handler
                [[NSURLConnection alloc] initWithRequest:request delegate:handler];
                
                //releases handler object
                [handler release];
                

                这两种方式都会将处理程序对象的所有权只保留给连接类,连接类将在处理程序对象完成加载、失败或被取消后立即释放,再次导致没有内存泄漏.

                Both ways will leave the handler object ownership only with the connection class, which will release the handler object right after it finishes loading, fails, or is canceled, once again resulting in no memory leaks.

                通过执行上述任何选项,您不必担心在 connection:DidFinishLoadingconnection 中释放委托(但您仍然必须释放连接):didFailWithError 方法.

                By doing any of the options above you don't have to worry about releasing the delegate (but you still have to release the connection) in connection:DidFinishLoading and connection:didFailWithError methods.

                这篇关于何时在 NSURLConnection 委托上调用 release?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将在 Interface Builder 中创建的 UITabBarController 设置为委托 下一篇:iOS如何知道哪个按钮被按下了带有自定义委托的CollectionView

                相关文章

                1. <small id='lzqHQ'></small><noframes id='lzqHQ'>

                  1. <legend id='lzqHQ'><style id='lzqHQ'><dir id='lzqHQ'><q id='lzqHQ'></q></dir></style></legend>

                    <tfoot id='lzqHQ'></tfoot>

                    • <bdo id='lzqHQ'></bdo><ul id='lzqHQ'></ul>
                    <i id='lzqHQ'><tr id='lzqHQ'><dt id='lzqHQ'><q id='lzqHQ'><span id='lzqHQ'><b id='lzqHQ'><form id='lzqHQ'><ins id='lzqHQ'></ins><ul id='lzqHQ'></ul><sub id='lzqHQ'></sub></form><legend id='lzqHQ'></legend><bdo id='lzqHQ'><pre id='lzqHQ'><center id='lzqHQ'></center></pre></bdo></b><th id='lzqHQ'></th></span></q></dt></tr></i><div id='lzqHQ'><tfoot id='lzqHQ'></tfoot><dl id='lzqHQ'><fieldset id='lzqHQ'></fieldset></dl></div>