• 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
Re: Another memory question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Another memory question


  • Subject: Re: Another memory question
  • From: Jörn Salewski <email@hidden>
  • Date: Tue, 26 Aug 2003 21:49:48 +0200

"option" 2 is even worse. You will have a memory leak _and_ the app will
crash.

> NSString *demoFile;
>
> NSString *temp = [[NSString alloc] init];

You're allocating memory for an immutable NSString and initializes it with
nothing.

> temp = [NSString stringWithString: @"~/Application User Data/dsf.plist"];

To the same memory region you assign an autoreleased NSString - the return
value of your convenience method. The previously allocated and initialized
String will not be accessible anymore --> a LEAK

> demoFile = [temp stringByExpandingTildeInPath];

demoFile will hold the reference to an autoreleased NSString

> [demoFile release];

You explicitly release the autoreleased string
> // more code

How much more code? It will crash.


I can't say exactly what will happen in your first code snippet, but why not
simply write:

NSString *demoFile;
NSString *tmp;
tmp = [NSString stringWithString: @"~/Application User Data/dsf.plist"];
demoFile = [tmp stringByExpandingTildeInPath];

(Or even shorter ?

NSString *demoFile = [[NSString stringWithString: @"~/Application User
Data/dsf.plist"] stringByExpandingTildeInPath];

)

Chears,

JS
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Another memory question
      • From: Andreas Mayer <email@hidden>
    • Re: Another memory question
      • From: "Alastair J.Houghton" <email@hidden>
References: 
 >Another memory question (From: James Ludtke <email@hidden>)

  • Prev by Date: Re: putting structs in NSArray
  • Next by Date: Re: Another memory question
  • Previous by thread: Another memory question
  • Next by thread: Re: Another memory question
  • Index(es):
    • Date
    • Thread