在iOS / iPhone:"打开的文件太多&QUOT ;:需要列出打开的文件(如lsof的) [英] on iOS/iPhone: "Too many open files": need to list open files (like lsof)

查看:214
本文介绍了在iOS / iPhone:"打开的文件太多&QUOT ;:需要列出打开的文件(如lsof的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想是这样的BSD命令lsof的,这,当然,是不是在iOS 4的使用,至少不会给我。理想的情况是C或ObjC功能。或工具,如鲨鱼或仪器。只需要为我们的应用程序运行的文件,而不是(与lsof的)所有的应用程序/进程。

我们做各种各样的事情与文件和code,它与打开的文件太多失败并没有在时代变了,因为情节不寻常的,这可能已在几个月前蹑手蹑脚。所以,没有必要提醒我要看看打开文件,并确保我关闭它们code。我已经知道了。将是很好的东西lsof的去年秋季将它缩小。谢谢你。


解决方案

 #进口< SYS / types.h中>
#进口< fcntl.h>
#进口<&errno.h中GT;
#进口<了sys / param.h>+(无效)lsof的
{
    诠释标志;
    INT的fd;
    焦炭BUF [MAXPATHLEN + 1];
    INT N = 1;    为(FD = 0; FD≤(INT)FD_SETSIZE; FD ++){
        错误号= 0;
        标志=的fcntl(FD,F_GETFD,0);
        如果(标志== -1放大器;&安培;错误号){
            如果(错误!= EBADF){
                返回;
            }
            其他
                继续;
        }
        使用fcntl(fd,F_GETPATH​​,BUF);
        的NSLog(@使用了文件描述符%D号%D:%S,FD,N,BUF);
        ++ N;
    }
}

We've discovered our complex iPhone app (ObjC, C++, JavaScript/WebKit) is leaking file descriptors under unusual circumstances.

I need to know which files (by file path) we are leaving open.

I want something like the BSD command "lsof", which, of course, isn't available in iOS 4, at least not to me. Ideally a C or ObjC function. Or a tool, like shark or Instruments. Just need the files for our running app, not (as with lsof) for all apps/processes.

We do all sorts of things with files, and the code that is failing with "Too many open files" hasn't changed in ages, and since the circumstances are unusual, this could have crept in months ago. So there's no need to remind me to look at code that opens files and make sure I close them. I know that already. Would be nice to narrow it down with something lsof-esque. Thanks.

解决方案

#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 ; 
    }
}

这篇关于在iOS / iPhone:&QUOT;打开的文件太多&QUOT ;:需要列出打开的文件(如lsof的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆