Вопрос

Часть этого приложения - кнопка «Scream», которая играет случайные крики из литых членов телешоу. Я должен немного ударить на приложение, чтобы увидеть утечку памяти в инструментах, но иногда идет вверх (каждые 45 секунд до 2 минут.) Утечка составляет 3,50 кБ, когда это происходит. Не смог взломать его в течение нескольких часов. Любая помощь оценила.

Инструменты говорит, что это напуганная линия кода:

[appSoundPlayer play]; 

Это связано с линейкой 9 следа стека ниже:

0 libsystem.b.dylib malloc.
1 libsystem.b.dylib pthread_create.
2 Audiooolbox Captheread :: Start ()
3 Audiooolbox GenericRunloopThread :: Start ()
4 AudioToolbox Audioqueuenew (Bool, AudiOstreambasicdescription Const *, Tcacallback Const &, CacallbackTarget Const &, Fungenced Long, Opaqueudioqueue **)
5 Audiooolbox Audioqueuenewoutput
6 AVFoundation AllocaudioQueue (AvaudioPlayer *, AudioPlayerImPl *)
7 AVFoundation Attemetoplayqueue (AvaudioPlayer *, AudioPlayerImpl *)
8 AVFoundation - [AvaudioPlayer BeortetoPlay
9 Queens Cream - [ScreamViewController Cream)
10 Corefoundation - [NSObject PerformentSelector: withoubject: withobject:
11 Uikit - [Отправка Uiacaplication: to: from: freevent:
12 UIKIT - [Отдача Uiagiccape: TotArget: Preysender: Frevent:
13 Uikit - [UICONTROL DENDACTION: TO: FOVET:
14 UIKIT - [UICONTROL (Внутренний) _sendctionsForevents: Covent:
15 UIKIT - [UICONTROL TARKESSEDED: CONEVENT:
16 Uikit - [Uiwindow _sendtouchesforevent:
17 Uikit - [Uiwindow SendEvent:
18 Uikit - [UiagicPlation SendEvent:
19 UIKIT _UIAPLICATIONHALDLEEVENT.
20 графических сервисов PurpleeventCallback
21 Corefoundation Cfrunlooprunspecific
22 Corefoundation Cfrunloopruninmode.
23 Графические сервисы гьевентрунмодал
24 UIKIT - [UiagicPlation _Run
25 Uikit UiacaplicationMain
26 Queens Queens Main / пользователи / ноутбук2 / настольные / закрепленные версии / ScreamQueEens25 / Cream Queens / Main.m: 14
27 Cream Queens Start

Вот .h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface ScreamViewController : UIViewController <UIApplicationDelegate,        AVAudioPlayerDelegate, MFMailComposeViewControllerDelegate> {





//AudioPlayer related
AVAudioPlayer           *appSoundPlayer;
NSURL                   *soundFileURL;
BOOL                    interruptedOnPlayback;
BOOL                    playing;

//Scream button related
IBOutlet UIButton       *screamButton;
int                     currentScreamIndex;
NSString                *currentScream;
NSMutableArray          *screams;
NSMutableArray          *personScreaming;
NSMutableArray          *photoArray;
int                     currentSayingsIndex;
NSString                *currentButtonSaying;
NSMutableArray          *funnyButtonSayings;
IBOutlet UILabel        *funnyButtonSayingsLabel;
IBOutlet UILabel        *personScreamingField;
IBOutlet UIImageView    *personScreamingImage;



//Mailing the scream related
    IBOutlet UILabel        *mailStatusMessage;
    IBOutlet UIButton       *shareButton;


}
//AudioPlayer related
@property (nonatomic, retain)   AVAudioPlayer                   *appSoundPlayer;
@property (nonatomic, retain)   NSURL                           *soundFileURL;
@property (readwrite)           BOOL                            interruptedOnPlayback;
@property (readwrite)           BOOL                            playing;

//Scream button related
@property (nonatomic, retain)   UIButton                        *screamButton;
@property (nonatomic, retain)   NSMutableArray                  *screams;
@property (nonatomic, retain)   NSMutableArray                  *personScreaming;
@property (nonatomic, retain)   NSMutableArray                  *photoArray;
@property (nonatomic, retain)   UILabel                         *personScreamingField;
@property (nonatomic, retain)   UIImageView                     *personScreamingImage;
@property (nonatomic, retain)   NSMutableArray                  *funnyButtonSayings;
@property (nonatomic, retain)   UILabel                         *funnyButtonSayingsLabel;


//Mailing the scream related
@property (nonatomic, retain) IBOutlet UILabel                  *mailStatusMessage;
@property (nonatomic, retain) IBOutlet UIButton                 *shareButton;


//Scream Button
- (IBAction)    scream:                 (id) sender;

//Mail the scream
- (IBAction) showPicker:    (id)sender;
- (void)displayComposerSheet;
- (void)launchMailAppOnDevice;


@end

Вот вершина .m:

#import "ScreamViewController.h"

//top of code has Audio session callback function for responding to audio route changes (from Apple's code), then my code continues...

@implementation ScreamViewController 

@synthesize appSoundPlayer;             // AVAudioPlayer object for playing the selected scream
@synthesize soundFileURL;               // Path to the scream
@synthesize interruptedOnPlayback;      // Was application interrupted during audio playback
@synthesize playing;                    // Track playing/not playing state


@synthesize screamButton;               //Press this button, girls scream.
@synthesize screams;                    //Mutable array holding strings pointing to sound files of screams.
@synthesize personScreaming;            //Mutable array tracking the person doing the screaming
@synthesize photoArray;                 //Mutable array holding strings pointing to photos of screaming girls
@synthesize personScreamingField;       //Field updates to announce which girl is screaming.
@synthesize personScreamingImage;       //Updates to show image of the screamer.
@synthesize funnyButtonSayings;         //Mutable array holding the sayings
@synthesize funnyButtonSayingsLabel;    //Label that updates with the funnyButtonSayings


@synthesize mailStatusMessage;          //did the email go out
@synthesize shareButton;                //share scream via email

Следующая строка начинает блок с оскорбительным кодом:

- (IBAction) scream: (id) sender
{
    //Play a click sound effect
    SystemSoundID soundID;
    NSString *sfxPath = [[NSBundle mainBundle]
                         pathForResource:@"aClick" ofType:@"caf"];    

    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:sfxPath],&soundID);
    AudioServicesPlaySystemSound (soundID); 


    // Because someone may slam the scream button over and over, 
    //must stop current sound, then begin next  
    if ([self appSoundPlayer] != nil)
    {
        [[self appSoundPlayer] setDelegate:nil];
        [[self appSoundPlayer] stop];
        [self setAppSoundPlayer: nil];

    }


    //after selecting a random index in the array (did that in View Did Load), 
    //we move to the next scream on each click. 

    //First check...
    //Are we past the end of the array?

    if (currentScreamIndex == [screams count]) 
    {
        currentScreamIndex = 0;
    }


    //Get the string at the index in the personScreaming array

    currentScream = [screams objectAtIndex: currentScreamIndex]; 


    //Get the string at the index in the personScreaming array
    NSString *screamer = [personScreaming objectAtIndex:currentScreamIndex];

    //Log the string to the console
    NSLog (@"playing scream: %@", screamer);

    // Display the string in the personScreamingField field
    NSString *listScreamer = [NSString stringWithFormat:@"scream by: %@", screamer];

    [personScreamingField setText:listScreamer];


    // Gets the file system path to the scream to play.
    NSString *soundFilePath = [[NSBundle mainBundle]    pathForResource:    currentScream
                                                              ofType:               @"caf"];

    // Converts the sound's file path to an NSURL object
    NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    self.soundFileURL = newURL;
    [newURL release];
    [[AVAudioSession sharedInstance] setDelegate: self];
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

    // Registers the audio route change listener callback function
    AudioSessionAddPropertyListener (
                                     kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                     self
                                     );

    // Activates the audio session.

    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error: &activationError];

    // Instantiates the AVAudioPlayer object, initializing it with the sound
    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];

    //Error check and continue
    if (newPlayer != nil)
    {
        self.appSoundPlayer = newPlayer;
        [newPlayer release];
        [appSoundPlayer prepareToPlay];
        [appSoundPlayer setVolume: 1.0];
        [appSoundPlayer setDelegate:self];
        //NEXT LINE IS FLAGGED BY INSTRUMENTS AS LEAKY
        [appSoundPlayer play];
        playing = YES;



        //Get the string at the index in the photoArray array
        NSString *screamerPic = [photoArray objectAtIndex:currentScreamIndex];

        //Log the string to the console
        NSLog (@"displaying photo: %@", screamerPic);

        // Display the image of the person screaming
        personScreamingImage.image = [UIImage imageNamed:screamerPic];

        //show the share button
        shareButton.hidden = NO;

        mailStatusMessage.hidden = NO;
        mailStatusMessage.text = @"share!";



        //Get the string at the index in the funnySayings array
        currentSayingsIndex = random() % [funnyButtonSayings count];
        currentButtonSaying = [funnyButtonSayings objectAtIndex: currentSayingsIndex]; 

        NSString *theSaying = [funnyButtonSayings objectAtIndex:currentSayingsIndex];
        [funnyButtonSayingsLabel setText: theSaying];

        currentScreamIndex++;



    }
}

Вот мой диск:

- (void)dealloc {
    [appSoundPlayer stop];
    [appSoundPlayer release], appSoundPlayer = nil;
    [screamButton release], screamButton = nil;
    [mailStatusMessage release], mailStatusMessage = nil;
    [personScreamingField release], personScreamingField = nil;
    [personScreamingImage release], personScreamingImage = nil;
    [funnyButtonSayings release], funnyButtonSayings = nil;
    [funnyButtonSayingsLabel release], funnyButtonSayingsLabel = nil;
    [screams release], screams = nil;
    [personScreaming release], personScreaming = nil;
    [soundFileURL               release];

    [super dealloc];
}


@end

Большое спасибо за чтение этого далеко! Любой ввод ценится.

Это было полезно?

Решение

Возможно, это красная сельдь от инструмента утечек. Смотрите этот пост в DEV Forums: https://devforums.apple.com/message/119423#119423.Я «обнаруживаю» одну и ту же утечку в инструменте утечек в моей реализации AvaudioPlayer, и я уверен, что мой код правильный (в основном потому, что это так просто). Вот фактический экспертный комментарий; Он говорит о том, что он обнаружил 3.5K в [NSTHREAD MANT], и у нас здесь 3,5 к [Capthread :: start ()], но я думаю, что мы в одной лодке:

«Этот вопрос состоит в том, что 3.5K вы видите, в основном, в основном структура данных, необходимая для ядра для отслеживания потока. Эта структура данных будет освобождена в конечном итоге, но после завершения потока больше нет ссылки на него в вашем процессе (только в ядре). "

Надеюсь, это поможет.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top