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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:09:45+00:00 2026-05-16T16:09:45+00:00

if I run this code: NSString *xml = [[NSString alloc] init]; xml=[NSString stringWithFormat:@<%@>, @msg];

  • 0

if I run this code:

NSString *xml = [[NSString alloc] init];
xml=[NSString stringWithFormat:@"<%@>", @"msg"];
NSLog(@"%@\n",xml);
[xml release];

I got:
2010-09-07 11:45:15.523 test[1312:207] <msg>
2010-09-07 11:45:15.527 test[1312:207] *** -[CFString release]: message sent to deallocated instance 0x3d52ba0

I have red the memory guide and I think that:
if i create an object using the alloc & init methods, I should release that object, but the rule does not play well here, why?

UPDATED

it seems that this line xml=[NSString stringWithFormat:@"<%@>", @"msg"]; is the problem.
I replaced it by xml=@"something"; and it worked.

any idea why I can not use the stringWithFormat method here?

UPDATED
Thanks for the answers @(Douwe Maan, BoltClock, Rod)

I have updated the code, just testing another way:

NSString *xml = [[NSString alloc] initWithFormat,@"<%@>", @"msg"];
NSLog(@"%@\n",xml);
[xml release];

interesting: if i run that code with the debug option enabled i get a message saying:
*** -[CFString _cfTypeID]: message sent to deallocated instance 0x3954ab0

but if i run it without the debugging tool, it never sends a message saying that.

questions:
1. is it normal that the error message will not appear if i am not running the project with the debug tool?
2. so, what is the problem with initWithFormat?, is it the same that has stringWithFormat? (i think it does not create an autoreleased instance)
3. how may i know that some method returns an autoreleased instance?, is there some naming convention anywhere?

  • 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-16T16:09:45+00:00Added an answer on May 16, 2026 at 4:09 pm

    Both @"some string" (apparently not, read @BoltClock’s second comment) and [NSString stringWithFormat:@"some string with object: %@", someObject] return an autoreleased instance of NSString. You can use this object any way you like, but you don’t own it (since you didn’t alloc, copy or retain it), so you don’t have to and shouldn’t release it yourself.

    Also, in your code, you actually have two NSString objects, the one you alloc‘d (on line 1) and the one that is alloc‘ed for you when you call stringWithFormat:, on line 2. On line 2 you’re re-pointing the variable xml to the second NSString, but the first one is still around in memory, so you’re creating a memory leak (as you haven’t released it)! You didn’t have to alloc this first one anyway, as your second NSString is the only one you’re actually using.

    So the correct code would be, getting rid of the release call and the first NSString:

    NSString *xml = [NSString stringWithFormat:@"<%@>", @"msg"];
    NSLog(@"%@\n", xml);
    

    Questions:

    1. Hmm, I’m not sure why that error would occur in the first place…
    2. init... should be called after alloc‘ing something, so it’s always [[Object alloc] initWithSomething]. The autorelease-version of this is [Object objectWithSomething], which does essentially this: return [[[Object alloc] initWithSomething] autorelease].
    3. You don’t really need to know what does and does not return an autoreleased object, just what you own, and should thus release, and do not own. You own everything you alloc, copy or retain, and you do not own everything else.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run this code: - (NSString *)description{ return [NSString stringWithFormat:@(FROG idle:%i animating:%i rect:%@
When I run this code, About half-way through the concatenation loop, $xml becomes null
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I run this code: - (void)unitButtonButtonTapped:(id)sender { [_label setString:@Last button: Unembossed square]; MilitaryUnits *target
Why this code don't work,when i want run this code vwd 2008 express show
I'm trying to run this code but this error appear: Uncaught TypeError: string is
When I run this code, the output is 11, 10. Why on earth would
When I run this code to call the Google API, all I get is
When I run this code $sql_select = INSERT INTO `database`.`table`(Columns) VALUES (Values)... ; $mysqlid
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]

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.