• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Retain count in non ARC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Retain count in non ARC


  • Subject: Retain count in non ARC
  • From: Varun Chandramohan <email@hidden>
  • Date: Fri, 04 Apr 2014 03:22:02 +0000
  • Thread-topic: Retain count in non ARC

Hi Guys,

I was looking into an older project which does not use ARC and I noticed new static code analysis issues with leaks. Here is a little sample code of the object in question.


@interface TempConfig : NSObject


@property (assign) NSMutableArray* contents;


+(id)newTempConfigWithSource:(NSString*)source;


@end


+ (id) newTempConfigWithSource:(NSString*)source

{

    ScanConfig* config = [[[self class] alloc] init];



    // Read contens from file

    NSError *errorDesc = nil;

    NSPropertyListFormat format;

    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:source];


    if( plistXML == nil ) {

        [config release];

        return nil;

    }

    // Serialize to array

    [config setContents:(NSMutableArray *)[[NSPropertyListSerialization

                                          propertyListWithData:plistXML

                                          options:0

                                          format:&format

                                          error:&errorDesc] retain]];

    if( [config contents] == nil ) {    <-------Static analysis points out to potential leak of object.

        [config release];

        return nil;

    }

    return config;

}


The way I use the code.

TempConfig* innerPList = [TempConfig newTempConfigWithSource:fullPath ];

[innerPList release];


Interestingly, the static code analysis find the call of "retain" as a leak. How do I address this issue? What is correct way to rewrite this code to make static analyser understand that I will be releasing the resource? If I removed the retain count, the warning goes away but it contents is freed and I crash.


Regards,

Varun

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Retain count in non ARC
      • From: John McCall <email@hidden>
    • Re: Retain count in non ARC
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: NSOperation Issues
  • Next by Date: Re: Retain count in non ARC
  • Previous by thread: Re: NSOperation Issues
  • Next by thread: Re: Retain count in non ARC
  • Index(es):
    • Date
    • Thread