Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6249129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:10:54+00:00 2026-05-24T13:10:54+00:00

Possible Duplicate: Objective C Memory Management My code is showing a memory leak here:

  • 0

Possible Duplicate:
Objective C Memory Management

My code is showing a memory leak here:

NSMutableArray* newImageArray = [[NSMutableArray alloc] init];
NSMutableArray* newMediaArray = [[NSMutableArray alloc] init];

if (self.categoryIndex == 0) {
    for (int i=1; i < [categoryArray count]; i++)
    {
        newImageArray = [NSMutableArray arrayWithArray:[newImageArray arrayByAddingObjectsFromArray:[self getImageArrayByCategoryIndex:i]]];
    }
}
else {
    newImageArray = [self getImageArrayByCategoryIndex:self.categoryIndex];
}

for (int i=0; i < [newImageArray count]; i++)
{
    Media* media = [[Media alloc] init];
    NSString* imageFile = [newImageArray objectAtIndex: i];

    media.imageFile = [UIImage imageNamed:imageFile];
    media.imageLabel = [[imageFile lastPathComponent] stringByDeletingPathExtension];
    media.soundFile = [appFolderPath stringByAppendingString:[[[imageFile stringByDeletingPathExtension] stringByAppendingString: @".wav"] stringByReplacingOccurrencesOfString: IMAGES_FOLDER withString: SOUNDS_FOLDER]];

    [newMediaArray addObject:media];
}

self.mediaArray = newMediaArray;

[self setNextMediaIndex];

I am not releasing media because it is being used by newMediaArray (which is used by mediaArray, which is used my my main object). Shouldn’t everything get released when I release my main object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:10:54+00:00Added an answer on May 24, 2026 at 1:10 pm

    It looks like you are leaking all over the place in a variety of ways

    newImageArray gets allocated but never released, additionaly you are overwriting the version that you allocated in the first line of you code with another version. So even if you released it at the end of this code segment, the wrong version would get released. It looks like you don’t even need to allocate this one.

    newMediaArray gets allocated but never released, you assign it to a property mediaArray if you are using @synthesize to create the code for that property, depending on how you declared that property, the setter will retain the value i.e. newMediaArray creating a leak.

    media gets allocated but never released, it get added to a NSMutableArray which means it will get retained by the array. If your app crashes when you release media in the for loop the problem is somewhere else

    The Memory Management Programming Guide is pretty much a must read

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to convert from int to string in objective c: example code…
Possible Duplicate: MD5 algorithm in Objective C I need to hash a string using
Possible Duplicate: Calling Python from Objective-C I'm a long-time Python programmer and short-time Cocoa
Possible Duplicate: Generating Random Numbers in Objective-C How do I generate a random number
Possible Duplicate: How do I concatenate strings in Objective-C? Firstly, the platform is iPhone
Possible Duplicate: How do I concatenate strings in Objective-C? I have this: [NSURL URLWithString:
Possible Duplicate: What are best practices that you use when writing Objective-C and Cocoa?
Possible Duplicate: Best practices for validating email address in Objective-C on iOS 2.0? I
Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.