代码之家  ›  专栏  ›  技术社区  ›  iwasrobbed

如何找到装入malloc 32kb的漏水水龙头

  •  5
  • iwasrobbed  · 技术社区  · 16 年前

    我一直在处理泄密问题,试图找出哪个功能没有被释放(我还是这个领域的新手),并且可以真正使用一些经验丰富的洞察力。

    我有一点代码似乎是罪魁祸首。每当我按下调用此代码的按钮时,32kb的内存会额外分配给内存,当释放该按钮时,内存不会被释放。

    我发现每一次 AVAudioPlayer 调用以播放M4A文件,解析M4A文件的最后一个函数是 MP4BoxParser::Initialize() 这反过来又通过 Cached_DataSource::ReadBytes

    我的问题是,如何在完成后重新分配,这样它就不会每次按下按钮时都分配32kb?

    非常感谢您提供的任何帮助!

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    //stop playing
    theAudio.stop;
    
    
    // cancel any pending handleSingleTap messages 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap) object:nil];
    
    UITouch* touch = [[event allTouches] anyObject]; 
    
    
    NSString* filename = [g_AppsList objectAtIndex: [touch view].tag];
    
    NSString *path = [[NSBundle mainBundle] pathForResource: filename ofType:@"m4a"];  
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
    theAudio.delegate = self; 
    [theAudio prepareToPlay];
    [theAudio setNumberOfLoops:-1];
    [theAudio setVolume: g_Volume];
    [theAudio play];
    }
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   James Eichele Bernard Igiri    16 年前

    Cocoa内存管理的诀窍是平衡 alloc , retain copy 随后调用 release .

    在这种情况下,您正在发送 同种异体 初始化您的 theAudio 变量,但您永远不会发送 释放 .

    假设一次只播放一个声音,最好的方法是使用控制器上的一个属性(拥有该属性的属性 -touchesBegan 方法)。属性声明如下所示:

    @property (nonatomic, retain) AVAudioPlayer * theAudio;
    

    然后你需要设置 音频 nil 在你 init 方法:

    theAudio = nil; // note: simple assignment is preferable in init
    

    确保释放 dealloc 方法:

    [theAudio release];
    

    现在,你 touchesBegan 可能如下所示:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        //stop playing
        theAudio.stop;
        ...
        AVAudioPlayer * newAudio = [[AVAudioPlayer alloc] initWithContentsOfUrl:...];
        self.theAudio = newAudio; // it is automatically retained here...
    
        theAudio.delegate = self; 
        [theAudio prepareToPlay];
        [theAudio setNumberOfLoops:-1];
        [theAudio setVolume: g_Volume];
        [theAudio play];
    
        [newAudio release];       // ...so you can safely release it here
    }
    
        2
  •  1
  •   Stephen Melvin    16 年前

    这句话在我看来是罪魁祸首:

    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
    

    这个资源什么时候释放?