标签: memory-management

从de AddressBook使用CFStrings时的内存管理

我正在使用Apple提供的API从AddressBook中读取记录.

我仍在考虑内存管理,所以CFStrings此刻让我感到困惑.

这就是我获取属性的方式:

//Get Basic properties
NSString* firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSNumber* record = [NSNumber numberWithInt:ABRecordGetRecordID(person)];

//Build Full Name
NSString* fullName=[self fullNameWith:firstName and:lastName];

//Get Phone number and Label
ABMultiValueRef phone = ABRecordCopyValue(person, property);
    //Turn identifier into index
CFIndex index = ABMultiValueGetIndexForIdentifier(phone, identifier);
    //Get the value and the label using the index
NSString *value =(NSString *)ABMultiValueCopyValueAtIndex(phone, index);
CFStringRef label = ABMultiValueCopyLabelAtIndex(phone, index);
    //Get the localized value of hte label
NSString * localizedLabel …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch memory-management cfstring addressbook

0
推荐指数
1
解决办法
272
查看次数

- [NSString componentsSeparatedByString:]泄漏

我有以下代码:

NSString *indexText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (indexText==nil) {
    [indexText release];
    indexText = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
[data release];
NSAutoreleasePool *innerPool = [NSAutoreleasePool new];
NSArray *packageList = [indexText componentsSeparatedByString:@"\n\n"]; //if commented out, there's no leak
[indexText release];
[innerPool drain];
Run Code Online (Sandbox Code Playgroud)

我正在执行componentsSeparatedByString:打开indexText,但是我泄漏了相当多的内存,尽管这packageList是自动释放的事实(事实证明,如果我尝试再次释放它,代码会崩溃).当我注释掉这条线时componentsSeparatedByString:,泄漏消失了.

顺便说一句,我正在运行iPhone模拟器时在Activity Monitor中查看内存使用情况; 仪器没有检测到泄漏.泄漏只是一种幻觉,模拟器的特殊性?

iphone cocoa-touch memory-management objective-c

0
推荐指数
1
解决办法
2867
查看次数

如何坚持NSColor?它没有分配,例如UIColor alloc

我正在尝试将工作的iPhone代码移植到Mac(iOS到OSX - 我相信?)

工作的iPhone版本是

...
return [[UIColor alloc] initWithRed:r green:g blue:b alpha:1.0f];
}
Run Code Online (Sandbox Code Playgroud)

不工作的Mac尝试是

...
return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
}                                       
Run Code Online (Sandbox Code Playgroud)

但是当我后来访问容器时,NSColor就不存在了.但是当我尝试各种版本的[NSColor alloc]时,它们都没有"工作".

我的问题是,我如何创建一个持久的NSColor(以后,我必须取消分配)?

macos memory-management allocation objective-c

0
推荐指数
1
解决办法
843
查看次数

内存泄漏问题NSAutoreleaseNoPool()

我正在尝试创建一个不可变的字符串.我没有使用init,alloc或new初始化它,但仍然内存泄漏,并且它说"类NSCFString的对象0x234b533自动释放,没有池到位 - 只是泄漏"这里是我想要做的

NSMutableString *srn = [NSMutableString stringwithCString:devSID];

// devSID is *char
Run Code Online (Sandbox Code Playgroud)

这留下了泄漏.我也尝试过这个

NSMutableString *srn = [NSMutableString stringwithCString:devSID length:sizeof(devSID)];
Run Code Online (Sandbox Code Playgroud)

但是这也行不通,但是如果我尝试用这样的简单字符串初始化它

NSMutableString *srn = @"this is my string";
Run Code Online (Sandbox Code Playgroud)

它有效,没有任何想法发生什么事情.我没有使用init或alloc,但仍然存在泄漏.如果有人能帮助我解决这个问题,我将不得不承担责任

问候

Umair

memory-leaks memory-management objective-c nsautoreleasepool

0
推荐指数
1
解决办法
1126
查看次数

如果数组是否保留count = 0,我们如何检查?

如果阵列在内存中,我们如何检查?

我有条件需要它

以下代码是否正确以释放数组

if (array1)
{
[array1 release];
}
Run Code Online (Sandbox Code Playgroud)

谢谢

memory-management objective-c

0
推荐指数
1
解决办法
311
查看次数

UIView是否会在Cocoa Touch中发布其子视图?

如果我在运行时创建一个UIControl并将其添加到视图中(通过addSubview :),视图是否会释放它,或者我应该这样做?

这是一个示例代码:

-(IBAction) cloneMe: (id) sender{

    if (!currentY) {
        currentY = [sender frame].origin.y;
    }

    UIButton *clone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CGRect cloneFrame = [sender frame];
    cloneFrame.origin.y += currentY + cloneFrame.size.height + 30;
    clone.frame = cloneFrame;
    [clone setTitle:@"I'm a clone" forState:UIControlStateNormal];

    [[sender superview] addSubview:clone];

    currentY = cloneFrame.origin.y + cloneFrame.size.height;


}
Run Code Online (Sandbox Code Playgroud)

cocoa cocoa-touch memory-management objective-c uiview

0
推荐指数
1
解决办法
626
查看次数

C++ - 关于内存管理的问题

我有以下课程:

class StringHolder
{
public:
    StringHolder(std::string& str)
    {
        m_str = &str;
    }

private:
    std::string* m_str;
};
Run Code Online (Sandbox Code Playgroud)

并拥有以下字符串object(str),大小为1,024KB:

char c = 'a';
unsigned long long limit = 1024 * 1024;
std::stringstream stream;
for(int i = 0; i < limit; i++)
{
    stream << c;
}
std::string str = stream.str();
Run Code Online (Sandbox Code Playgroud)

每当我用字符串初始化StringHolder类时,它都不会复制该字符串.那是因为我使用了引用和指针,但我不确定我是否正确使用它们:

问题:我是否正确使用了引用和指针?

c++ memory-management

0
推荐指数
1
解决办法
159
查看次数

目标C中的内存管理

我有一个关于引用计数的问题.

这是我的构造函数:

- (id)initWithId:(NSString *)graphId;
Run Code Online (Sandbox Code Playgroud)

在另一个类中,我通过以下方式创建一个对象:

GraphViewController *graph =  
[[GraphViewController alloc] initWithId:[[NSString alloc] initWithFormat:@"%s", current_ID];
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何正确释放字符串对象?

释放作为参数传递的字符串是否正确?

iphone memory-management objective-c

0
推荐指数
1
解决办法
169
查看次数

NSArray的Objective C内存管理问题

我正在加载一个像这样的浮点数组:

NSArray *arr= [NSArray arrayWithObjects:
                [NSNumber numberWithFloat:1.9],
                [NSNumber numberWithFloat:1.7],
                [NSNumber numberWithFloat:1.6],
                [NSNumber numberWithFloat:1.9],nil];
Run Code Online (Sandbox Code Playgroud)

现在我知道这是正确的做法,但我对零售数量感到困惑.

  1. 每个Object都由该[NSNumber numberWithFloat:]方法创建.这给对象保留计数为1 dosnt吗? - 否则该物体将被回收

  2. arrayWithObjects:方法向每个对象发送保留消息.

这意味着每个对象的保留连续为2.当取消分配数组时,每个对象都被释放,保留计数为1.

我错过了什么?

memory-management objective-c

0
推荐指数
1
解决办法
434
查看次数

Objective-C内存管理问题

好的,如果我有这个:

NSString *lastPushed = (NSString *)[tagStack objectAtIndex:[tagStack count]-1];
.//do something with the last pushed element locally
.//do more cstuff here
.//after doing all the stuff needed in the function

[lastPushed release];
Run Code Online (Sandbox Code Playgroud)

其中tagStack是NSMutableArray

如果我发布了lastPushed,因为它没有被复制或者被引入,它是否只会释放这个引用,还是会实际释放mutableArray中的对象?

memory-management objective-c

0
推荐指数
1
解决办法
62
查看次数