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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:15:55+00:00 2026-06-10T06:15:55+00:00

I has written following code and then run. In after, When I touch uibutton,

  • 0

I has written following code and then run.
In after, When I touch uibutton, this app is terminated.

I want to know why terminated.

I doubt if autorelease?

Are there anyone who can explicitly explain

why myClass instance is released and

where myClass is released and

the way that myClass can use autorelease?

    @interface MyClass : NSObject
    - (void)testMethod;
    @end

    @implementation MyClass{
        NSMutableArray *array;
    }

    - (id)init{
        if ((self = [super init])) {
            array = [[NSMutableArray alloc] initWithCapacity:0];
        }
        return self;
    }

    - (void)dealloc {
        [array release];
        [super dealloc];
    }

    - (void)testMethod {
        NSLog(@"after init : %@", array);
    }

    @end

    @implementation ViewController {
        MyClass *myClass;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        myClass = [[[MyClass alloc] init] autorelease];  <== cause of ternimate?

        UIButton *aButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton addTarget:self action:@selector(testArray:) forControlEvents:UIControlEventTouchUpInside];
        aButton.frame=CGRectMake(110.0f, 129.0f, 100.0f, 57.0f);
        [aButton setTitle:@"title" forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
        [self.view addSubview:aButton];

    }

    - (void)testArray:(id)sender {
        [myClass testMethod];
    }
@end
  • 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-06-10T06:15:56+00:00Added an answer on June 10, 2026 at 6:15 am

    Seems that due to

    myClass = [[[MyClass alloc] init] autorelease];
    

    the instance of MyClass is already deallocated when you touch the button. Don’t be lazy – autorelease is not an ultimate ‘solve my memory management problem and make me not have to think’ solution – here you need to use just

    myClass = [[MyClass alloc] init];
    

    then release it manually when needed – probably in your ViewController class’ -dealloc method.

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

Sidebar

Related Questions

I have this application written in JSF 2.0 facelets and it has the following
Once code has been written, the only way I know of to view the
I am reviewing another developer's code and he has written a lot of code
My app has been written for SDK 1.5 3.0 but when it is running
Consider the following code (written with Visual Studio 2010 and .NET 4.0) using System;
I have the following code written to make my lazy second CPU core working.
I am using Struts 2. I have a JSP. It has this code: <span
I've written the following code to do a binary search for a value, target
The following code was written in vbscript and I'm in the process of converting
A colleague of mine has written a DLL file for me to use called

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.