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 6939151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:35:06+00:00 2026-05-27T12:35:06+00:00

First of all, sorry for the amount of code. What i’m doing wrong managing

  • 0

First of all, sorry for the amount of code.
What i’m doing wrong managing memory. I can’t understand why the analyzer throws a memory leak.

@interface obj : NSObject 
{
    NSMutableArray *array;
}
@property (retain, nonatomic) NSMutableArray *array;
@end

@implementation obj
@synthesize array;

- (id)init
{
    self = [super init];
    if (self) 
    {
        // Initialization code here.
        array = [[NSMutableArray alloc] init];
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
    [array release];
}

@end

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    obj *test = [[obj alloc] init];
    NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];

    NSLog(@"Número: %g \n", [numero floatValue]);

    [test.array addObject:numero];

    NSLog(@"Numero de elementos: %lu", [test.array count]);

    NSLog(@"Valor: %g", [[test.array objectAtIndex:0] floatValue]);

    NSLog(@"Numero de elementos t2: %lu", [test.array count]);

    numero = [NSNumber numberWithFloat:5.8];

    NSLog(@"Valor t2: %g", [[test.array objectAtIndex:0] floatValue]);
    NSLog(@"Número t2: %g \n", [numero floatValue]);

    [test.array addObject:numero];    

    NSLog(@"Valor: %g", [[test.array objectAtIndex:0] floatValue]);

    [numero release]; **<-- Leak of memory**
    [test release];
    [pool drain];

    return 0;
}
  • 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-27T12:35:06+00:00Added an answer on May 27, 2026 at 12:35 pm

    Easy fix, you forgot to release the existing value before reassigning it.

    // You created an instance of NSNumber here
    NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];
    
    // Then you reassigned it here without releasing it first which caused the leak HERE
    numero = [NSNumber numberWithFloat:5.8];
    
    [numero release]; **<-- Leak of memory**
    

    You can get around this completely by using numberWithFloat in both instances which returns an autoreleased object.

    NSNumber *numero = [NSNumber numberWithFloat:3.4];
    
    numero = [NSNumber numberWithFloat:5.8];
    
    // Now you don't need to release it at all ;)
    //[numero release]; **<-- Leak of memory**
    

    Or you can fix your existing example by:

    NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];
    
    [numero release];
    
    numero = [NSNumber numberWithFloat:5.8];
    
    // Remove this one since numberWithFloat returns an autoreleased object
    //[numero release]; **<-- Leak of memory**
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, first off all sorry, i can't login using my yahoo provider. anyways
first of all sorry if this isn't formatted correctly, first time doing this. I've
First of all sorry for my language. I am a doing a shopping cart
First of all sorry for my bad english. I'm a german guy. The code
Well, first of all sorry about this question it must be pretty straight forward
first of all, sorry if that question is dumb but I´m a total newbie
First of all, sorry about that title. I'm not the best at writing those
First of all, sorry if this has been asked before. I've done a pretty
First of all, sorry if this isn't an appropriate question for StackOverflow. I've tried
First of all, I'm fairly new to Java, so sorry if this question is

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.