iOS 内存泄漏处理


问题1:loadNibNamed

1
2
3
4
5
6
static NSString *cellId = @"AboutTableViewCell";
AboutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"AboutTableViewCell" owner:self options:nil] lastObject];
}

原因:I suspect that your leak may be coming from the outlets in the nib. Note this phrase in the docs on loadNibNamed::
To establish outlet connections, this method uses the setValue:forKey: method, which may cause the object in the outlet to be retained automatically.
In other words, loadNibNamed sometimes imposes an extra retain because of the odd way key-value-coding works.

However, that is speculation, and there’s no need for it, because there’s no need for you to call loadNibNamed: in the first place!

You’re using a custom UITableViewCell subclass, designed in a nib? Then why not do this the normal way? Make a nib containing one top-level object: the cell. Design the cell in the nib, set its class, hook up its outlets etc. In your code, call registerNib:forCellReuseIdentifier: on the table view, to tell the table view about your nib. When you later call dequeueReusableCellWithIdentifier:, if there are no free cells in the reuse pile, the table view will load your nib and hand you cell. No muss, no fuss.

问题2:imageWithRenderingMode

1
tabBarItem1.selectedImage = [[UIImage imageNamed:@"img_wifi_hl"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

暂时未找到解决办法

问题3:id bridge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (id)fetchSSIDInfo

{

NSArray *ifs = CFBridgingRelease(CNCopySupportedInterfaces());
id info = nil;
for (NSString *ifnam in ifs) {
info = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam));
if (info && [info count]) {
break;
}
}
return info;
}

CFBridgingRelease 替换掉原来的(id bridge)

LintCode 最大间距


http://www.lintcode.com/zh-cn/problem/maximum-gap/

最大间距 查看运行结果

给定一个未经排序的数组,请找出其排序表中连续两个要素的最大间距。

如果数组中的要素少于 2 个,请返回 0.

您在真实的面试中是否遇到过这个题? Yes
样例
给定数组 [1, 9, 2, 5],其排序表为 [1, 2, 5, 9],其最大的间距是在 5 和 9 之间,= 4.

注意
可以假定数组中的所有要素都是非负整数,且最大不超过 32 位整数。

挑战
排序十分简单,但将使用 O(nlogn) 时间复杂度。请避免使用线性时间复杂度和空间。

##代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
/**
* @param nums: an array of integers
* @return: the maximum difference
*/

public int maximumGap(int[] nums) {
if(null == nums || nums.length < 2)
{
return 0;
}

int length = nums.length;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

for(int i = 0;i < length;i++)
{
if(nums[i] < min)
{
min = nums[i];
}

if(nums[i] > max)
{
max = nums[i];
}
}
// 桶的概念
int bucketCapacity = (max - min) / length + 1;
int bucketNumber = (max - min) / bucketCapacity + 1;
int[] minBuckets = new int[bucketNumber];
int[] maxBuckets = new int[bucketNumber];
int maxDistance = 0;

for(int i = 0;i < bucketNumber;i++)
{
minBuckets[i] = Integer.MAX_VALUE;
maxBuckets[i] = Integer.MIN_VALUE;
}

for(int i = 0;i < length;i++)
{
int index = (nums[i] - min) / bucketCapacity;
if(minBuckets[index] > nums[i])
{
minBuckets[index] = nums[i];
}

if(maxBuckets[index] < nums[i])
{
maxBuckets[index] = nums[i];
}
}

int pre = 0;
for(int i = 1;i < bucketNumber;i++)
{
if(minBuckets[i] == Integer.MAX_VALUE && maxBuckets[i] == Integer.MIN_VALUE)
continue;
if(minBuckets[i] - maxBuckets[pre] > maxDistance)
{
maxDistance = minBuckets[i] - maxBuckets[pre]; // 因为最大间距不可能在一个桶内,否则bucketNumber * maxDistance > length;
}
pre = i;
}
return maxDistance;
}
}