Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 汉明距离计算中strtoull()调用内存泄漏_Objective C_Macos_Hash_Memory Leaks_Strtoull - Fatal编程技术网

Objective c 汉明距离计算中strtoull()调用内存泄漏

Objective c 汉明距离计算中strtoull()调用内存泄漏,objective-c,macos,hash,memory-leaks,strtoull,Objective C,Macos,Hash,Memory Leaks,Strtoull,我在命令行Objective-CosX应用程序中调用strtoull()超过1亿次,计算汉明距离。我已经从ph_hamming_distance()中找到了此函数调用的~30字节/调用内存泄漏。我查看了strtoull()的BSD源代码,甚至删掉了我不需要的通用性,将源代码放在我的应用程序中,但仍然存在内存泄漏 呼叫代码为: NSArray * returnMatchedImagesFromDB(NSString * hash, NSString * asin, NSInteger ac

我在命令行Objective-CosX应用程序中调用strtoull()超过1亿次,计算汉明距离。我已经从ph_hamming_distance()中找到了此函数调用的~30字节/调用内存泄漏。我查看了strtoull()的BSD源代码,甚至删掉了我不需要的通用性,将源代码放在我的应用程序中,但仍然存在内存泄漏

呼叫代码为:

    NSArray * returnMatchedImagesFromDB(NSString * hash, NSString * asin, NSInteger action) {

        /*  Input hash, asin, action(not yet used)
         *  Calculate Hamming Distance to all records in DB
         *  Return NSArray of HammingDistanceRecords of matches within "hdCompareThreshold" of each other
         */ 
        int  hd;
        int threshold = 0;
        NSMutableArray * retArray = [[NSMutableArray alloc] init];

        threshold = hdCompareThreshold;

        // for each image in dbImageArray, compute hamming distance to all other images
        for (ImageRecord *imgRecord in dbImageArray) {
            hd = ph_hamming_distance(imgRecord.hash, hash);
            if ((threshold == -1) || (hd <= threshold)) {
                HammingDistanceRecord * hdRec = [[HammingDistanceRecord alloc] init];
                hdRec.hammingDistance = hd;
                hdRec.asin1 = asin;
                hdRec.asin2 = imgRecord.asin;
                hdRec.rank2 = imgRecord.rank;
                [retArray addObject:hdRec];
            }
        }
        return [retArray copy];
    }   // returnMatchedImagesFromDB()

int ph_hamming_distance(NSString * hashStr1,NSString * hashStr2) {

            NSUInteger hash1 = strtoull([hashStr1 UTF8String],NULL,0);
            NSUInteger hash2 = strtoull([hashStr2 UTF8String],NULL,0);
            NSUInteger x = hash1^hash2;
            const NSUInteger m1  = 0x5555555555555555ULL;
            const NSUInteger m2  = 0x3333333333333333ULL;
            const NSUInteger h01 = 0x0101010101010101ULL;
            const NSUInteger m4  = 0x0f0f0f0f0f0f0f0fULL;
            x -= (x >> 1) & m1;
            x = (x & m2) + ((x >> 2) & m2);
            x = (x + (x >> 4)) & m4;
            return (x * h01)>>56;
        }
NSArray*returnMatchedImagesFromDB(NSString*hash,NSString*asin,NSInteger操作){
/*输入哈希、asin、操作(尚未使用)
*计算到所有记录的汉明距离(DB)
*返回彼此“hdCompareThreshold”内匹配的HammingDistance记录数组
*/ 
int-hd;
int阈值=0;
NSMutableArray*retArray=[[NSMutableArray alloc]init];
阈值=hdCompareThreshold;
//对于dbImageArray中的每个图像,计算到所有其他图像的汉明距离
用于(dbImageArray中的ImageRecord*imgRecord){
hd=ph_-hamming_距离(imgRecord.hash,hash);
如果((阈值==-1)| |(hd>1)&m1;
x=(x&m2)+(x>>2)和m2);
x=(x+(x>>4))&m4;
返回(x*h01)>>56;
}
ph_hamming_distance()的参数始终是base10(没有alpha字符)。典型的hashStr是@“17609976980814024116”。我正在比较的对象数据库当前是390K个对象,因此所有对象与自身的内部比较是3000亿次对strtoull()的调用。漏洞导致我的应用程序在每次约3500次比较时SIGKILL-9。这是3500*390K*2次调用/比较=~80 GB,这是我驱动器上的可用空间,因此我猜当交换文件填满驱动器时,OS X正在终止进程


非常感谢您的帮助。

可能是您的
[hashStr1 UTF8String]
调用,这将分配一个
char*
缓冲区,在您的自动释放上下文清理之前不会释放,这可能是“从不”如果您在循环中调用所有这些,而不返回到
nsrunlop
。例如,请参见

,我的猜测是,它不仅仅是
strtoull
。您能否显示您的循环,该循环也在执行对
ph\u hamming\u distance
的调用?但命令行程序没有nsrunlop,是吗?Mayb我将尝试创建一个显式的char*p1=[hashStr1 UTF8String],将p发送到strtoull(p,NULL,0),然后执行p=nil。这是否允许释放缓冲区?似乎这个问题应该被许多人发送字符串到某个f(string)的人所碰到.p=nil将无法释放任何内容。请查看是否可以找到手动排空autorelease池的方法。也许可以放置一个
@autorelease{…}
阻止内部循环?请参见@rick,这就是问题所在;命令行程序没有运行循环,您调用的代码需要运行循环或手动管理自动释放池。@Faffafff您的自动释放块建议成功了!处理25000条记录后,内存大小恒定在95MB。这在fa中是ct是我的其他命令行程序的一个解决方案,这些命令行程序的足迹越来越大。谢谢!!也许我应该多阅读苹果文档,而不是将搜索限制在StackOverflow:-)对不起,我没有足够的分数来投票支持你的建议。