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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:40:38+00:00 2026-05-25T06:40:38+00:00

I’m new to Objective-C, and I’ve read some memory management articles but am having

  • 0

I’m new to Objective-C, and I’ve read some memory management articles but am having trouble.

I hava a class something like this:

-(UIWebView*)getWebView{
    //UIWebView* webview = [UIWebView initWithFrame:self.frame];  edited,the original wrong line
    UIWebView* webview = [[UIWebView alloc] initWithFrame:self.frame]; 
    return webview; 
}
-(void)addToSelf{
    UIWebView* view = [self getWebView];
    [self addSubview:view];
    [view release];  //release here
}

In my thought, objc objects are all like C pointers (it is ?)
And thinking like this:

UIWebView* webview = [[UIWebView alloc] initWithFrame:self.frame];  //retain +1
UIWebView* view = [self getWebView];  //just a pointer to the same object ?
[self addSubview:view]; //retain +1
[view release];   //retain -1

Now view’s retainCount = 1.
Then this viewController will handle the webview’s life cycle.

There must be something wrong with my thought (sure also the code),
but I don’t know why .

UIWebView* webview = [[[UIWebView alloc] initWithFrame:self.frame] autorelease];

When I remove the last release line, code works fine, why?
What’s the difference?

————–edit line—————

Few minutes ago , there’s an answer about it but it’s disappeared , now I rewrite my thought:

The answer says:

When returning object from method , i must use autorelease to tell compiler I have end with it, and then using addSubview , finish (no need releasing).

I know this is right , but why it’s right ?

In most of codes:

-(void)someMethod{
    UIWebView* webview = [[UIWebView alloc] initWithFrame:self.frame]; 
    [self addSubview:webview];
    [webview release];
}

this works fine, but when separate the code to two methods like the top ,it’s not.

Why i must use autorelease when returning object?

  • 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-25T06:40:39+00:00Added an answer on May 25, 2026 at 6:40 am

    It looks as if you don’t alloc the UIWebView:

    UIWebView* webview = [[UIWebView alloc] initWithFrame:self.frame];
    

    That means you probably don’t have a valid UIWebView, and a retain count that is undefined, since the code for UIWebView is actually using some unknown piece of memory. If this works, you are lucky.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.