- 浏览: 580718 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
JYY282:
[i][/i]
Ecshop与Shopex的比较 -
qq247890212:
我也遇见这问题了,真诡异。重新下载个猫换了就好了。 太浪费时间 ...
诡异:ClassNotFoundException: org.springframework.web.filter.CharacterEncoding
From: http://www.raywenderlich.com/2696/how-to-debug-memory-leaks-with-xcode-and-instruments-tutorial
This is the second article in a three-part series on working with memory in Objective-C on the iPhone. In the first part of the series, we covered how to manage memory in Objective-C by using instance variables and reference counting.
No matter how well you understand memory management in Objective-C, from time to time you’re bound to make mistakes. But often there’s way too much code to search line-by-line for problems (unless you want your hair to turn gray!)
Luckily, Apple has provided some great ways to help you find memory-related problems in your applications. Sometimes these tools scare new developers, but they’re actually pretty awesome and easy to use!
That’s what this tutorial is all about. You’ll get hands hands-on experience using XCode and Instruments to debug and detect memory related problems.
This tutorial assumes you are familiar with memory management in Objective-C. If you are still shaky on the subject, you may wish to read the memory management tutorial first.
Getting Started
Our goal in this tutorial is to check for and resolve any memory leaks in an example app that illustrates common memory-related mistakes. So to get started, download a leaky app that I’ve put together for this tutorial.
Open up the app and run it in XCode. You’ll see a list of sushi in a table view. Try selecting several rows, and then – BOOM! You get the dreaded EXC_BAD_ACCESS error, and the debugger is no help:
This can be very frustrating to many beginning developers, as it’s not clear where the problem is. Here’s the advice I generally give to developers when you hit an EXC_BAD_ACCESS error:
- Set the NSZombieEnabled argument in your executable options, which sometimes helps narrow down the cause
- Run with Apple Instruments such as Leaks to look for memory issues
- Set a breakpoint in your code and step through until you narrow down where it’s crashing
- Tried and true “comment out code till it works” then backtrack from there :]
So let’s try this out for ourselves by trying option #1 – turning on NSZombieEnabled.
Zombie Invasion!
Unfortunately, the NSZombieEnabled option has nothing to do with the zombie apocalypse, so you can put away your boomsticks and chainsaws :]
NSZombieEnabled is a flag that you can enable that will provide a warning when you try to access an object that has been deallocated. And since accessing deallocated memory is one of the most common reasons for crashing, this is a good thing to try first.
To set this up, expand the Executables group in your sidebar in XCode, and double click the PropMemFun executable. Select the Arguments tab, go to the “Variables to be set in the environment” section, and click the Plus button. Set the name of the variable to NSZombieEnabled, and set the value to YES, as follows:
Now run the app, and click on a few rows again until it crashes. Go to your console log, and you’ll see the following message:
2011-02-03 12:07:44.778 PropMemFun[27224:207] *** -[CFString respondsToSelector:]: message sent to deallocated instance ...
The program will also halt on the exact line where it’s crashing now. You can go up the backtrace to find the exact line where it’s crashing by selecting the first area in the backtrace that is your code – in this case tableView:didSelectRowAtIndexPath.
Aha! Now you know that in this line, a message is being sent to a deallocated string. This line uses two strings: _lastSushiSelected, and sushiString.
Well sushiString looks OK, because it’s initialized with stringWithFormat (which returns an autorelease variable), so it should be safe to use until the next run loop. But what about _lastSushiSelected?
_lastSushiSelected was set the last time this method was run to sushiString. But sushiString is an autorelease variable, so at some point it will be released, and the memory will be deallocated. But then _lastSushiSelected would still be pointing to deallocated memory! Which explains the problem – sending a message to deallocated memory causes a crash.
So to solve this, we just need to retain _lastSushiSelected so that the memory doesn’t go away. So replace the last line with the following:
_lastSushiSelected = [sushiString retain]; |
Compile and run your code, and now you should be able to run without crashing!
Build, Analyze, and Recognize
Ok, so we have an app that isn’t crashing – a good first step. But next, we need to start making sure that it isn’t leaking any memory.
There’s an easy way to perform an initial first-glance check on your app to see if it has any memory leaks or other problems – use the built-in Build and Analyze function.
This will make XCode run through your code and look for any mistakes it can automatically detect, and warn you about any potential problems. It doesn’t catch everything, but when it does catch things it’s a really quick and easy way to find out about the problems.
Give it a shot by selecting Build\Build and Analyze. You should see that it detected a memory leak, as you can see below:
The message says that there’s a potential leak related to the “alertView”. If you look at this line, you’ll see that the UIAlertView was created with a alloc/init (which returns an object with a reference count of 1), but never released! There are several ways to fix this, but one way is to add the following after [alertView show]:
[alertView release]; |
Go to Build\Build and Analyze again, and you’ll see that there are no remaining issues.
Leaks and Plumbers
Unfortunately, you can’t rely on Build\Build and Analyze to catch everything. There’s one other great automated tool to help you check your app for leaks – the Leaks Instrument.
Let’s try it out. Go to Run\Run with Performance Tool\Leaks, and select a few rows in the table view. Also scroll up and down the table view from the top of the table to the bottom of the table. After bait of experimentation, you should start seeing some leaks popping up in the Leaks tab, which show up as blue bars.
Click the stop button, then go to the toolbar in the middle and click it to change from “Leaked Blocks” to “Call Tree”. In the panel in the lower left, click “Invert Call Tree”, and “Hide System Libraries”. You’ll see that it found two different methods in the code with memory leaks, as you can see below:
If you double click a function name, it will take you directly to the line of code that creates the object that was leaked. This should give you a really good hint where the problem lies, and if you examine the code and think about it a bit, you should be able to figure out what the problem is and fix it.
So why not take a look at the code and see if you can figure out what the problem is and fix it? Once you’ve made a fix, you can run Leaks again and verify that the leak no longer occurs. Come back here once you’ve finished, or gotten stuck!
…
…waiting…
…
…waiitng…
…
…waiting…
…
What?! Are you still reading here?! You can do it – go ahead and try! :]
The Solution
tableView:didSelectRowAtIndexPath
Leaks tells us that the string created and stored into sushiString is leaked. So let’s think through why this is, step by step:
- When sushiString is created, it’s created with stringWithFormat. This returns an object with a retain count of 1, and a pending autorelease.
- In the last line in the method, you call retain on the sushiString (bumping the retain count up to 2) and store it into _lastSushiSelected.
- Later on, the autorelease takes effect, decrementing the retain count to 1.
- Next time the tableView:didSelectRowAtIndexPath method is called, you override the _lastSushiSelected variable with a pointer to a new string – without releasing the old one! So that old string still has a retain count of 1, and is never released.
One solution to this is to add the following line before setting lastSushiSelected to the sushiString:
[_lastSushiSelected release]; |
tableView:cellForRowAtIndexPath
Just like in the previous method, a string that’s created and stored into a variable named sushiString is leaked. Here’s what’s going on:
- A new string is created with alloc/init.
- This returns an object with a reference count of 1.
- However, this reference count is never decremented, so there’s a memory leak!
This could be solved in one of three ways:
- Call release on sushiString after setting the textLabel to the string.
- Call autorelease on sushiString after creating it with alloc/init.
- Instead of using alloc/init, use stringWithFormat, which returns a string already marked as autorelease.
Plumb the Leaks!
Fix the above two problems, compile and run Leaks again, and verify that you no longer have any leaks in the app!
Where To Go From Here?
Here is a sample project with the updated app, with no leaks or crashes.
At this point, you should have some good hands-on experience with how to find memory leaks in your app using NSZombieEnabled, Build and Analyze, and the Leaks Instrument. You should be able to apply these techniques to your projects to help find and resolve memory leaks!
Next check out the third article in this series, where I cover how to check your apps for memory leaks or memory-related mistakes, via using Instruments and other helpers.
If you have any other advice or tips to developers for good techniq
发表评论
-
Objective-C 与 C++ 的异同
2013-04-02 12:03 1396http://www.cnblogs.com/y041039 ... -
Cocos2D-X是全球知名的开源跨平台手机游戏引擎
2013-01-22 10:05 2752http://www.oschina.net/p/cocos ... -
iOS Keyboard 键盘高度变化 自适应
2013-01-15 15:43 3249[[NSNotificationCenter default ... -
iOS使用自定义字体
2012-11-27 12:11 12140From: http://blog.csdn.net/csy1 ... -
4 款类似 Facebook/Path 切换效果的 iOS 组件
2012-11-27 12:03 2184From: http://blog.csdn.net/lia ... -
Path 2.0的UI界面设计详细介绍
2012-11-27 11:56 1464如Path的创始人Dave Morin ... -
史上最全的App Store邮箱列表
2012-11-27 11:51 1269From: http://roybaby.blog.51cto ... -
iOS从info.plist 获取项目的名称及版本号
2012-11-16 10:54 1665From: http://blog.sina.com.cn/s ... -
MapKit annotation drag and drop with callout info update
2012-10-13 10:38 2405http://hollowout.blogspot ... -
NSArray 或NSDictionary 调用writeToFile方法失败原因
2012-08-31 10:03 4466NSArray 或NSDictionary 调用writeTo ... -
如何让IOS应用从容地崩溃
2012-08-30 15:25 1615From: http://www.cocoachina.com ... -
iOS中判断设备系统版本
2012-08-29 17:17 31710在iOS开发中,经常要考虑系统的向下兼容,如果使用 ... -
iOS 汉字转拼音
2012-08-21 16:42 1464From: http://www.cnblogs.com/v2 ... -
iOS模拟器截图工具
2012-08-17 16:35 1654From: http://magicalboy.com/ios ... -
XCode下的iOS单元测试
2012-08-10 17:47 1163From: http://mobile.51cto.com/ ... -
AFNetworking
2012-08-08 10:54 4648AFNetworking on github: https:/ ... -
Wrapping Conventions
2012-08-01 15:54 822Wrapping Conventions ... -
Core Animation如何使显式动画结束时的值直接作用Layer
2012-08-01 14:51 3793(1)使用隐式动画会直接改变layer的属性值,如: ima ... -
Using Properties in Objective-C Tutorial
2012-07-31 16:27 924From: http://www.raywenderlich. ... -
appstore 收款: 如何绑定收款的银行卡?
2012-07-27 09:07 9429From: http://www.oschina.net/qu ...
相关推荐
### Detected Memory Leaks 简单检测方法 #### 背景介绍 在软件开发过程中,内存泄漏是一个常见的问题,特别是在使用C/C++等需要手动管理内存的语言进行开发时更为突出。内存泄漏不仅会导致程序占用的内存持续增长...
标题 "How catch memory leaks with very little effort (7KB)" 提示了这是一个关于如何轻松检测内存泄漏的技术分享。内存泄漏是编程中的一个常见问题,尤其是在C++等不自动管理内存的语言中,程序员需要手动分配和...
### Xcode Instruments 使用详解 #### 一、Instruments 用户指南简介 **Instruments** 是苹果为开发者提供的一个强大的性能分析工具,它可以帮助开发者检测并优化应用程序的性能问题。该工具适用于 macOS 和 iOS ...
436 pages Publisher: Apress; 1 edition (July 3, 2013) Language: English ISBN-10: 1430249501 ISBN-13: 978-1430249504 What you’ll learn ...How to eliminate memory leaks and poor-performing code
You will begin with a basic understanding of memory management, and why memory leaks occur in an application, moving on to autorelease pools and object creation/storage to get an idea of how memory ...
《Pro iOS 5 Tools: Xcode Instruments and Build Tools》是一本深入探讨iOS开发的专业书籍,主要针对iOS 5版本,并且兼容Xcode 4.2和4.3这两个当时最新的开发工具。这本书全面介绍了用于iOS应用程序开发的Xcode集成...
### Pro iOS5 Tools, Xcode Instruments and Build Tools #### 关键知识点概述 1. **Xcode Overview** - **Xcode**: Xcode 是苹果官方提供的集成开发环境(Integrated Development Environment, IDE),主要用于...
本文将详细讲解如何在Xcode中使用内存泄露查找工具,比如Memory Leaks和Instruments。 首先,了解什么是内存泄露是必要的。内存泄露指的是程序在申请内存后,未能在不再使用时及时释放内存,导致可用内存逐渐减少,...
### Pro iOS 5 Tools - Xcode Instruments and Build Tools #### 关键知识点概览 1. **iOS应用开发挑战** - 应用崩溃 - 内存泄露 - 虫生虫(Bugs creating other bugs) 2. **Xcode工具箱** - **Xcode** - *...
Memory leaks occur when a program allocates memory but fails to release it back to the system, causing the application to consume more and more memory over time. This can lead to poor performance and ...
这篇“Finding memory leaks发现内存的泄漏”可能是关于如何定位和解决内存泄漏的技术指南。 在C++编程中,程序员需要手动管理内存,通过`new`关键字申请内存,然后通过`delete`关键字释放内存。如果忘记释放或错误...
Learn how to analyze CLR 4 .NET application and service crashes and freezes, navigate through memory dump space (managed and unmanaged code) and diagnose corruption, leaks, CPU spikes, blocked ...
Enhanced Memory Leak Detection ...The main difference between the CRT Debug Library and VLD, is that Visual Leak Detector shows you the complete callstack used for memory allocation has led to the leak.
标题《论文研究-A Tool for Testing Java Memory Leaks.pdf》和描述《一个测试Java内存泄漏的工具,党万春,钱巨,虽然Java语言有自带的垃圾回收机制,Java程序仍然存在由无效引用带来的内存泄漏。而内存泄漏会导致...
C++ Standard Library Quick Reference by...• What smart pointers are and how to use them to prevent memory leaks • How to write safe and efficient multi-threaded code using the C++11 threading libraries
Chapter 12, Audio in Game Development, explains how to add sound and music effects to games, and avoiding memory leaks while playing sounds. Chapter 13, Tips and Tricks, has some neat tips and tricks...
- **Memory Management:** Techniques for allocating and deallocating memory to avoid leaks. #### Cocoa Framework The Cocoa framework provides a rich set of APIs for building native Mac applications. ...
平常我们都会用 Instrument 的 Leaks / Allocations 或其他一些开源库进行内存泄露的排查,但是检查过程非常繁琐,而且不清晰,最主要的是Abandoned memory不会被检测出来。 Leaks 从苹果的开发者文档里可以看到,一...
* Use XCode’s powerful performance and debugging tools to eliminate memory leaks, zombies, and other hazards. * Understand the process for packaging your application for end-user distribution ...