gpt4 book ai didi

iphone - 在 iOS/iPhone 上 : "Too many open files": need to list open files (like lsof)

转载 作者:IT王子 更新时间:2023-10-29 08:14:17 24 4
gpt4 key购买 nike

我们发现我们复杂的 iPhone 应用程序(ObjC、C++、JavaScript/WebKit)在异常情况下会泄漏文件描述符。

我需要知道我们要打开哪些文件(按文件路径)。

我想要 BSD 命令“lsof”之类的东西,当然,它在 iOS 4 中不可用,至少对我来说是这样。理想情况下是 C 或 ObjC 函数。或工具,如 shark 或 Instruments。只需要我们正在运行的应用程序的文件,而不是(与 lsof 一样)所有应用程序/进程的文件。

我们对文件做了各种各样的事情,并且因“打开的文件太多”而失败的代码多年来一直没有改变,而且由于情况不寻常,这可能在几个月前就已经悄悄发生了。因此,无需提醒我查看打开文件的代码并确保关闭它们。我已经知道了。用 lsof-esque 缩小范围会很好。谢谢。

最佳答案

#import <sys/types.h>  
#import <fcntl.h>
#import <errno.h>
#import <sys/param.h>

+(void) lsof
{
int flags;
int fd;
char buf[MAXPATHLEN+1] ;
int n = 1 ;

for (fd = 0; fd < (int) FD_SETSIZE; fd++) {
errno = 0;
flags = fcntl(fd, F_GETFD, 0);
if (flags == -1 && errno) {
if (errno != EBADF) {
return ;
}
else
continue;
}
fcntl(fd , F_GETPATH, buf ) ;
NSLog( @"File Descriptor %d number %d in use for: %s",fd,n , buf ) ;
++n ;
}
}

关于iphone - 在 iOS/iPhone 上 : "Too many open files": need to list open files (like lsof),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4083608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com