V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
childs
V2EX  ›  iDev

在学习 iOS 开发时,使用 subdataWithRange:NSMakeRange 遇到一个问题

  •  
  •   childs · 2015-02-28 09:30:10 +08:00 · 8058 次点击
    这是一个创建于 3590 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在加载存到数据库中的pdf文件(大约14M)的二进制数据时,通过

    [data appendData:[self.archiveData subdataWithRange:NSMakeRange(self.offset, len)]];加载数据
    len是int类型offset是NSInteger类型。
    self.offset += len;
    但是当self.offset增加到差不多7M左右的时候就会报错。错误如下:
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(14082573) failed'
    *** First throw call stack:
    (
    0 CoreFoundation 0x06345946 exceptionPreprocess + 182
    1 libobjc.A.dylib 0x05fcea97 objc_exception_throw + 44
    2 CoreFoundation 0x0634586d +[NSException raise:format:] + 141
    3 Foundation 0x04f800ac NSAllocateMemoryPages + 106
    4 Foundation 0x04e8e643 _NSDataCreateVMDispatchData + 34
    5 Foundation 0x04e8c83a -[_NSPlaceholderData initWithBytes:length:copy:deallocator:] + 252
    6 Foundation 0x04e8c72d -[NSData(NSData) initWithBytes:length:copy:freeWhenDone:bytesAreVM:] + 92
    7 Foundation 0x04ee9363 -[NSData(NSData) copyWithZone:] + 144
    8 CoreFoundation 0x062c4ce9 -[
    NSCFData copyWithZone:] + 105
    9 Foundation 0x04f33531 -[NSSubrangeData initWithData:range:] + 484
    10 Foundation 0x04ef3727 -[NSData(NSData) subdataWithRange:] + 657
    11 ESPTablet 0x00edf137 -[CWHessianUnarchiver(Private) readDataWithTag:] + 189
    12 ESPTablet 0x00edf1b3 -[CWHessianUnarchiver(Private) readDataWithTag:] + 313
    13 ESPTablet 0x00edf1b3 -[CWHessianUnarchiver(Private) readDataWithTag:] + 313
    14 ESPTablet 0x00edf1b3 -[CWHessianUnarchiver(Private) readDataWithTag:] + 313
    15 ESPTablet 0x00edf1b3 -[CWHessianUnarchiver(Private) readDataWithTag:] + 313
    16 ESPTablet 0x00edf1b3 -[CWHessianUnarchiver(Private) readDataWithTag:] + 313
    17 ESPTablet 0x00edf1b3 -[CWHessianUnarchiver(Private) readDataWithTag:] + 313
    。。。。错误还有很多没有全贴出来

    这个错误好像是分配内存失败了
    请教各位大神,知道这是什么原因吗?

    7 条回复    2015-03-01 01:41:52 +08:00
    keithellis
        1
    keithellis  
       2015-02-28 09:59:49 +08:00
    内存泄露吧
    hyzjshwo
        2
    hyzjshwo  
       2015-02-28 10:03:16 +08:00   ❤️ 1
    恩 没有更多代码 我赌你是内存分配过大,中间产生的内存超过栈的上限了。。。要么修改栈大小,要么控制产生的临时内存空间。
    flicker317
        3
    flicker317  
       2015-02-28 10:20:38 +08:00
    @hyzjshwo 应该是堆吧
    hyzjshwo
        4
    hyzjshwo  
       2015-02-28 10:23:15 +08:00
    @flicker317 那要看他具体代码了
    childs
        5
    childs  
    OP
       2015-02-28 10:29:40 +08:00
    @hyzjshwo
    ESPTablet[8934:184080] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(14082573) failed'
    *** First throw call stack:
    (
    0 CoreFoundation 0x063b9946 __exceptionPreprocess + 182
    1 libobjc.A.dylib 0x06042a97 objc_exception_throw + 44
    2 CoreFoundation 0x063b986d +[NSException raise:format:] + 141
    3 Foundation 0x04ff40ac NSAllocateMemoryPages + 106
    4 Foundation 0x04f02643 _NSDataCreateVMDispatchData + 34
    5 Foundation 0x04f0083a -[_NSPlaceholderData initWithBytes:length:copy:deallocator:] + 252
    6 Foundation 0x04f0072d -[NSData(NSData) initWithBytes:length:copy:freeWhenDone:bytesAreVM:] + 92
    7 Foundation 0x04f5d363 -[NSData(NSData) copyWithZone:] + 144
    8 CoreFoundation 0x06338ce9 -[__NSCFData copyWithZone:] + 105
    9 Foundation 0x04fa7531 -[NSSubrangeData initWithData:range:] + 484
    10 Foundation 0x04f67727 -[NSData(NSData) subdataWithRange:] + 657
    11 ESPTablet 0x00f53134 -[CWHessianUnarchiver(Private) readDataWithTag:] + 218
    12 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    13 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    14 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    15 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    16 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    17 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    18 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    19 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    20 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    21 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    22 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    23 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    24 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    25 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    26 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    27 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    28 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    29 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    30 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    31 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    32 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    33 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    34 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    35 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    36 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    37 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    38 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    39 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    40 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    41 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    42 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    43 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    44 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    45 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    46 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    47 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    48 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    49 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    50 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    51 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    52 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    53 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    54 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    55 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    56 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    57 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    58 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    59 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    60 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    61 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    62 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    63 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    64 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    65 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    66 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    67 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    68 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    69 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    70 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    71 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    72 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    73 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    74 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    75 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    76 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    77 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    78 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    79 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    80 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    81 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    82 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    83 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    84 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    85 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    86 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    87 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    88 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    89 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    90 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    91 ESPTablet 0x00f531ba -[CWHessianUnarchiver(Private) readDataWithTag:] + 352
    目前只能贴这么多 剩下的错误跟这些类似
    hyzjshwo
        6
    hyzjshwo  
       2015-02-28 10:37:13 +08:00
    @childs 我不知道你要干什么事 但是这句代码暴露了你或者你们对内存管理上的一些缺陷
    [data appendData:[self.archiveData subdataWithRange:NSMakeRange(self.offset, len)]];
    这段在内存里会有两份pdf大小的空间。data这个变量来看你似乎只是临时使用,那你为什么要一段一段取self.archiveData 而不是直接使用呢?还有兄弟,如果是必现的bug都是最容易解决的bug。
    tuoxie007
        7
    tuoxie007  
       2015-03-01 01:41:52 +08:00
    看起来就是系统无法一次性申请到这么大的内存,因为你subdata出来的其实是个数组。总之大文件的处理最好不能一次性读到内存里,否则再多内存也不够你用,得分段处理数据流啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5365 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:39 · PVG 15:39 · LAX 23:39 · JFK 02:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.