gpt4 book ai didi

ios - 数组或 CGImageRef 的内存泄漏

转载 作者:行者123 更新时间:2023-11-29 10:52:45 24 4
gpt4 key购买 nike

我正在制作一个从电影中去除帧的程序。这在 iOS 7 上的 Xcode 5.0 模拟器中。我试图放入这个数组中的对象数量是 447。(似乎没有那么多)。

由于数组大小不够大导致内存泄漏,我的程序不断崩溃?? (不知道这种说法是否正确)或由于其他原因。仅对于帧数大于 350 的视频,它在 for 循环中始终失败。较少帧的视频工作正常。

只是预感这与我将大型原始帧图像存储到数组中这一事实有什么关系吗?我没有缩小框架图片?

CODE:
NSMutableArray* allFrames = [[NSMutableArray alloc] init];


// get each frame
for (int k=0; k< totalFrames; k++)
{

int timeValue = timeValuePerFrame * k;
CMTime frameTime;
frameTime.value = timeValue;
frameTime.timescale = movie.duration.timescale;
frameTime.flags = movie.duration.flags;
frameTime.epoch = movie.duration.epoch;

CMTime gotTime;

CGImageRef myRef = [generator copyCGImageAtTime:frameTime actualTime:&gotTime error:nil];
[allFrames addObject:[UIImage imageWithCGImage:myRef]];

if (gotTime.value != frameTime.value)
NSLog (@"requested %lld got %lld for k %d", frameTime.value, gotTime.value, k);

//cleanup?
CFRelease(myRef);



}

ERROR:
2013-11-03 20:18:24.746 test[42139:a0b] requested 9925 got 9921 for k 397
2013-11-03 20:18:25.122 test[42139:a0b] requested 9950 got 9946 for k 398
everseHD(42046,0xb029b000) malloc: *** mmap(size=8298496) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
2013-11-03 20:10:31.936 ReverseHD[42046:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x020305e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01db38b6 objc_exception_throw + 44
2 CoreFoundation 0x01fe2c3c -[__NSArrayM insertObject:atIndex:] + 844
3 CoreFoundation 0x01fe28e0 -[__NSArrayM addObject:] + 64
4 ReverseHD 0x00003131 -[v1ViewController extractImagesFromMovie] + 3089
5 libobjc.A.dylib 0x01dc5874 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x00b27c8c -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x00b27c18 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x00c1f6d9 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x00c1fa9c -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x00c1e815 -[UIControl touchesBegan:withEvent:] + 254
11 UIKit 0x00b64efb -[UIWindow _sendTouchesForEvent:] + 386
12 UIKit 0x00b65d34 -[UIWindow sendEvent:] + 1232
13 UIKit 0x00b39a36 -[UIApplication sendEvent:] + 242
14 UIKit 0x00b23d9f _UIApplicationHandleEventQueue + 11421
15 CoreFoundation 0x01fb98af __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x01fb923b __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x01fd630e __CFRunLoopRun + 910
18 CoreFoundation 0x01fd5b33 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x01fd594b CFRunLoopRunInMode + 123
20 GraphicsServices 0x032239d7 GSEventRunModal + 192
21 GraphicsServices 0x032237fe GSEventRun + 104
22 UIKit 0x00b2694b UIApplicationMain + 1225
23 ReverseHD 0x0000244d main + 141
24 libdyld.dylib 0x02975725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

内存泄漏 使用了大量内存

大量内存使用内存泄漏

您正确地发布了 myRef,您拥有它,因为它是使用 copy 方法创建的,因此您发布的代码没有任何泄漏。

您尝试存储 447 张图像只是在使用大量内存。

Doesn't seem that many

不好评价。您应该使用带有内存分配模板的 Instruments 分析应用程序并检查数组的大小,这可能会填满所有堆。

如果发现帧太大而无法存储在数组中,可以使用 AVAssetImageGeneratormaximumSize 属性减小生成帧的大小, 例如

generator.maximumSize = CGSizeMake(320, 180);

如文档中所述:

The default value is CGSizeZero, which specifies the asset’s unscaled dimensions.

关于ios - 数组或 CGImageRef 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19760292/

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