假设我们有一些Core Foundation对象,例如CGColorRef,添加到NSArray这样的对象:
CGColorRef color = ...;
NSArray *array = [NSArray arrayWithObject:(id)color];
Run Code Online (Sandbox Code Playgroud)
由于数组保留其内容,因此color收到retain消息(不是CFRetain()吗?).从内存管理的角度来看,在这种情况下会发生什么?
假设我正在编写自己的函数,该函数接收CFDataRef对象,对其执行某些操作,并返回另一个CFDataRef对象:
CFDataRef transformData(CFDataRef inData)
{
//Question 1: Should I call CFRetain(data) here to make sure it doesn't
//go away? (This of course would involve releasing data just before returning
//from this function, or as soon as I no longer need data.)
CFDataRef outData;
//Somehow produce the new outData from inData (and assume we are the
//owner of outData, since we created it right here).
//Question 2: What, if anything, should I do with outData before
//returning it? I'm unsure of …Run Code Online (Sandbox Code Playgroud) 我有这种方法(别人写了!)
- (CGPDFDocumentRef)getPdf {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myLocalFileName.pdf"];
NSURL *pdfURL = [NSURL fileURLWithPath:pdfPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
return pdf;
}
Run Code Online (Sandbox Code Playgroud)
现在,我运行了Analyze并获得三个内存泄漏警告:
Call to function 'CGPDFDocumentCreateWithURL' returns a Core Foundation object with a +1 retain count
Object returned to caller as an owning reference (single retain count transferred to caller)
Object leaked: object allocated and stored into 'pdf' is returned from a method whose name ('getPdf') does not start …Run Code Online (Sandbox Code Playgroud) 我需要计算MacOS中文件夹的大小.此大小值必须与Finder一致.我已经尝试了几种方法来做到这一点.但结果总是与Finder不同.
我试过以下方法.
typedef struct{
//getResourceValue in NSURL
unsigned long long fileSize;
unsigned long long fileAllocSize;
unsigned long long totalFileSize;
unsigned long long totalFileAllocSize;
//lstat in posix
unsigned long long statSize;
unsigned long long blockSize;
//NSFileManager
unsigned long long cocoaSizeNo;
unsigned long long cocoaSizeYes
}sizePack;
- (sizePack)foldSize:(NSString *)direString{
sizePack sizeP;
memset(&sizeP, 0, sizeof(sizeP));
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *tempArray = [fileManager contentsOfDirectoryAtPath:direString error:nil];
for (NSString *fileName in tempArray) {
BOOL flag = YES;
NSString *fullPath = [direString stringByAppendingPathComponent:fileName];
if ([fileManager …Run Code Online (Sandbox Code Playgroud) 我需要知道当前连接的网络接口的网络接口名称,如en0,lo0等.
是否有Cocoa/Foundation功能可以提供这些信息?
我正在试图弄清楚如何创建一个保留而不是复制其键的NSMutableDictionary.我已经实现了 - (NSUInteger)哈希和 - (id)isEqual:对于我想要的密钥,我只是弄清楚要在回调中指定哪些选项.
CFDictionaryKeyCallBacks keyCallbacks = { 0, NULL, NULL, CFCopyDescription, CFEqual, NULL };
self.commonParents = (NSMutableDictionary*)CFBridgingRelease(CFDictionaryCreateMutable(nil, 0, &keyCallbacks, &kCFTypeDictionaryValueCallBacks));
Run Code Online (Sandbox Code Playgroud)
上面的代码在ARC中正确使用对键的弱引用,但是如果我想要强引用呢?关键回调应该是什么样的?
我希望CFAllocator用我自己的实现替换我的iPhone应用程序中的默认值.我想控制分配的内存,UIWebView因为它似乎在加载一个网站后保留了这么多的内存,并且内存在UIWebView发布时仍然存在.
在我调用之后,我在下一次分配时CFAllocatorSetDefault遇到EXC_BREAKPOINT异常.
异常似乎发生在调用内部CFRetain(在模拟器中完成但在设备上发生同样的事情):
CoreFoundation`CFRetain:
0x1c089b0: pushl %ebp
0x1c089b1: movl %esp, %ebp
0x1c089b3: pushl %edi
0x1c089b4: pushl %esi
0x1c089b5: subl $16, %esp
0x1c089b8: calll 0x1c089bd ; CFRetain + 13
0x1c089bd: popl %edi
0x1c089be: movl 8(%ebp), %esi
0x1c089c1: testl %esi, %esi
0x1c089c3: jne 0x1c089db ; CFRetain + 43
0x1c089c5: int3
0x1c089c6: calll 0x1d66a00 ; symbol stub for: getpid <- EXC_BREAKPOINT (code=EXC_I386_BPT subcode=0x0)
0x1c089cb: movl %eax, (%esp)
0x1c089ce: movl $9, 4(%esp) …Run Code Online (Sandbox Code Playgroud) CGContextRef ctx = CGContextRetain([[NSGraphicsContext currentContext] graphicsPort]);
CGColorRef color = CGColorRetain([NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.5f].CGColor);
CGContextSaveGState(ctx);
{
CGContextSetFillColorWithColor(ctx, color);
CGContextFillRect(ctx, dirtyRect);
}
CGContextRestoreGState(ctx);
CGColorRelease(color);
CGContextRelease(ctx);
Run Code Online (Sandbox Code Playgroud) 将字符串解析为NSURL对象时,NSURL使用单个正斜杠将字符串处理为与方案后面带有双正斜杠的字符串不同.
为什么会这样?
以下是一些例子:
NSURL *url = [NSURL URLWithString:@"app-id://path1/path2"];
NSLog(@"%@",url.scheme); // "app-id"
NSLog(@"%@",url.path); // "/path2"
NSLog(@"%@",url.host); // "path1"
NSURL *url = [NSURL URLWithString:@"app-id:/path1/path2"];
NSLog(@"%@",url.scheme); // "app-id"
NSLog(@"%@",url.path); // "/path1/path2"
NSLog(@"%@",url.host); // nil
Run Code Online (Sandbox Code Playgroud) 有没有办法以编程方式拨打电话而不退出当前的应用程序?我做了一项研究,但所有答案都是
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Run Code Online (Sandbox Code Playgroud)
这不是我想要的,我还需要访问语音流.
core-foundation ×10
objective-c ×8
cocoa ×5
ios ×5
macos ×2
c ×1
ios7 ×1
iphone ×1
networking ×1
nsurl ×1
uiwebview ×1