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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:51:50+00:00 2026-05-22T01:51:50+00:00

A beginners question about how to be memory efficient when using an UIView which

  • 0

A beginners question about how to be memory efficient when using an UIView which contains a couple of images (ca. 500K). I guess if I handle this in the wrong way and call this view ten or twenty times, my app will crash (as I have leaked about 5-10 MB of RAM).

I have an UIView which I create programatically like so:

myView = [[UIView alloc] initWithFrame:0,0,0,0];

To this view I add a couple of images so that it eats up 500K of memory. After I’m done with this view, I’d like to free up the memory again. So I coded:

[myView removeFromSuperview];
myView = nil;
[myView release];

Is this the way to go? I am particularly uncertain about the last release call. Is myView not already released if I remove it from my superview and set it to nil?

Also, would it be a good idea to simply autorelease myView in the first instance, i.e.

myView = [[[UIView alloc] initWithFrame:0,0,0,0] autorelease];

I’d be grateful for any suggestions and corrections.

  • 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-22T01:51:51+00:00Added an answer on May 22, 2026 at 1:51 am

    You’re sending a release message to nil. The correct order for those statements would be:

    [myView removeFromSuperview];
    [myView release];
    

    and optionally after that:

    myView = nil;
    

    For discussion on why to set to nil:

    • Set pointers to nil after release?
    • Is It Necessary to Set Pointers to nil in Objective-C After release?
    • What’s the difference between setting an object to nil vs. sending it a release message in dealloc

    The superview retains your view when you add it as a subview, and then releases it when you remove it. You still need you release your hold of it. You could use autorelease when allocating it, but since you need to hold on to a pointer to it to be able to send removeFromSuperview, the correct way is to send release when you are done with that pointer (and then set that pointer to nil).

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

Sidebar

Related Questions

A beginners' question about foreign key in MySQL. In w3school it says, A FOREIGN
A niave beginners question about database design. I have an app managing some logger
I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in
I'm fairly new to C++ so this is probably somewhat of a beginner question.
We allocate memory in C using malloc and in C++ using new. I know
I guess this is very basic but since I'm learning .NET by myself, I
A beginner question, bear with me: I'm just wondering under what circumstances one should
A beginner's question. I'm building a .swf with Flex Ant. To my .swf I
Once again a very beginner-ish question, but here I go: I would like to
I am a beginner of python and have a question, very confusing for me.

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.