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

  • SEARCH
  • Home
  • 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 8990351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:29:00+00:00 2026-06-15T22:29:00+00:00

My code seems to be leaking when using C arrays and I’m not sure

  • 0

My code seems to be leaking when using C arrays and I’m not sure why.

/* LeakyClass.m */

@property (nonatomic, assign) char **array1;
@property (nonatomic, assign) id __strong *array2;
@property (nonatomic, assign) unsigned short currentDictCount;

//...

- (id)initWithCapacity:(unsigned short)capacity
{
    if ((self = [super init])) {
        _array1 = (char **)calloc(sizeof(char*), capacity);  //Leak
        _array2 = (id __strong *)calloc(sizeof(id), capacity);  //Leak
    }
    return self;
}

- (void)dealloc {
    free(_array1);
    free(_array2);
    _array1 = NULL;
    _array2 = NULL;
}

- (void)addObjectToArray2:(id)object andCharToArray1:(char *)str
{
    [self array2][[self currentDictCount]] = object;
    [self array1][[self currentDictCount]] = str;

    [self setCurrentDictCount:[self currentDictCount] + 1];
}

@end

I call LeakyClass with this:

/* OtherClass.m */

LeakyClass *leaky = [[LeakyClass alloc] initWithCapacity:20];
[leaky addObjectToArray2:object andCharToArray1:"1"];  // Leak
[leaky addObjectToArray2:@"example" andCharToArray1:"2"];  /Leak
[leaky addObjectToArray2:[NSURL URLWithString:exampleString] andCharToArray1:"3"];  /Leak

Instruments are pointing to each of the values passed onto the LeakyClass to add to array 1. In this example, object, @"example" and [NSURL URLWithString:exampleString]. Instruments is also pointing to the callocs for _array1 and _array2 but I free both of them in dealloc.

Am I missing something?

  • 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-06-15T22:29:01+00:00Added an answer on June 15, 2026 at 10:29 pm

    First of all using __strong with an id pointer is meaningless, ARC will try to send the release, retain etc messages to the pointer, not to each of you objects (given your current implementation it doesn’t even know how many objects you dynamically allocated) , so, in short terms, you’re the same with or without it. As for the leak, try to free your pointers before alloc.

    Anyways, as others mentioned, why not use NSArray or/and NSDictionary?

    Edit following the comments below:

    When I said “you’re the same with or without it” I meant, it will not help you in any way with your memory management, it’s meaningless. However, you have to have an ownership qualifier, so just removing it will end up in an error (as you reported). You should use

    @property (nonatomic, assign) id __unsafe_unretained *array2;
    

    As for your memory problem, did you tried to free before alloc? What happens if initWithCapacity: gets called twice, I know you won’t call it twice, but there’s no guarantee for instruments to know.

    Also here are the specs for you to understand better ARC and all the qualifiers.

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

Sidebar

Related Questions

The following javascript code seems to leak memory but I’m not sure why. I’ve
The code seems not working. // $counter is an instance of Zend_Db_Table_Abstract $counter->update(array('hits' =>
My code seems to run but it does not display the result any result
The following code seems not to work, even though the file appears to be
It seems to me that tmpfs is not re-using inode numbers, but instead creates
This code seems to work well, modifying the properties of my button, except the
My code seems to do infinite recursion when I invoke it with trees of
The following code seems to be just too much, for getting a single count
The below code seems like it should work; however, the blob in the database
The following piece of code seems to unreliably execute and after and undeterministic time

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.