菜鸟教程小白 发表于 2022-12-12 15:42:39

ios - 如何修复 Objective-C 中的内存泄漏?


                                            <p><p>我构建了一个从 HockeyApp 获取报告的简单应用程序。但是,当我使用内存泄漏工具运行应用程序时,它显示当我执行 getReport 操作时存在内存泄漏。我无法理解仪器中显示的所有信息。</p>

<p>这是导致内存泄漏的按钮操作方法:</p>

<pre><code>- (IBAction)getReports:(id)sender {

//initialize url that is going to be fetched.
NSURL *url = ;

//initialize a request from url
NSMutableURLRequest *request = ;
;

;
;

//initialize a connection from request
NSURLConnection *connection = [ initWithRequest:request delegate:self];
self.getReportConnection = connection;

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data{

    if (connection==getReportConnection) {

   ;

   NSLog(@&#34;data is %@&#34;,data);

    NSString *responseString = [ initWithData:data encoding:NSUTF8StringEncoding];

    NSError *e = nil;
    NSData *jsonData = ;

    NSDictionary *JSON = ;
    NSLog(@&#34;login json is %@&#34;,JSON);
   NSLog(@&#34;reason json is %@&#34;,JSON[@&#34;reason&#34;]);

    enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {


      ];
         NSLog(@&#34;index = %lu, Object For title Key = %@&#34;, (unsigned long)idx, obj[@&#34;reason&#34;]);
    }];

    NSError *error = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData
                                                         options:kNilOptions error:&amp;error];

    if (error != nil) {
      NSLog(@&#34;Error parsing JSON.&#34;);
    }
    else {
      NSLog(@&#34;Array: %@,array count is %d&#34;, jsonArray,jsonArray.count);
    }

   // ];

    if (JSON!=NULL) {
      UIAlertView *alert=[initWithTitle:@&#34;Reports succesfully retrieved&#34; message:@&#34;&#34; delegate:self cancelButtonTitle:@&#34;Ok&#34; otherButtonTitles: nil];
      ;
    }

       }
}

// This method receives the error report in case of connection is not made to server.
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

UIAlertView *errorAlert=[initWithTitle:@&#34;Wrong Login&#34; message:nil delegate:self cancelButtonTitle:@&#34;ok&#34; otherButtonTitles: nil];
;
NSLog(@&#34;error is %@&#34;,error);
}

// This method is used to process the data after connection has made successfully.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

}
</code></pre>

<p>我发现内存泄漏发生在 <code>didRecieveData</code> 方法中出现警报 View 之前。</p>

<p>这里是内存泄漏工具显示内存泄漏的截图:</p>

<p> <img src="/image/No2CA.png" alt="enter image description here"/> </p>

<p>我不明白代码的哪一部分导致了内存泄漏。谁能告诉我如何使用泄漏工具识别导致内存泄漏的代码部分?</p>

<p><strong>编辑:</strong>当我在模拟器上运行应用程序时,仪器没有显示任何内存泄漏:</p>

<p>截图如下:
<img src="/image/Cr6ci.png" alt="enter image description here"/> </p>

<p>当我在设备上运行应用程序时,仪器再次显示内存泄漏:
<img src="/image/YfJVn.png" alt="enter image description here"/> </p>

<p>我查看了泄漏部分,发现 <code>NSmutableArray</code> 导致了泄漏:
<img src="/image/JozSl.png" alt="enter image description here"/>
<img src="/image/ngOwq.png" alt="enter image description here"/> </p>

<p>我在我的代码中只使用了一个 <code>NSMutableArray</code>。我在 <code>.h</code> 文件中声明了它:</p>

<pre><code>@property (nonatomic,strong) NSMutableArray *reportArray;
</code></pre>

<p>并在<code>viewDidLoad</code>中分配:</p>

<pre><code>reportArray=[init];
</code></pre>

<p>并将其加载到 <code>didRecieveData</code>:</p>

<pre><code> ];
</code></pre>

<p>堆栈跟踪快照:</p>

<p> <img src="/image/Tez4i.png" alt="enter image description here"/>
<img src="/image/iZAMK.png" alt="enter image description here"/>
<img src="/image/vzfz3.png" alt="enter image description here"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这个:</p>

<pre><code>reportArray = [[ init] autorelease];
</code></pre>

<p>在您的 <code>connectionDidFinishLoading:</code> 和 <code>connection:didFailWithError:</code> 方法中设置</p>

<pre><code>reportArray = nil
</code></pre>

<p>最后在 <strong>Project > Build Phases > Compile Sources</strong> 添加 <code>-fno-objc-arc</code> 作为编译器标志<strong>此文件</strong> <em> (已编辑,抱歉)</em>。然后再次点击 Product menu >Analyze <em>(command + shift + B)</em>,查看是否还存在内存泄漏。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何修复 Objective-C 中的内存泄漏?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31208743/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31208743/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何修复 Objective-C 中的内存泄漏?