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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:32:32+00:00 2026-05-27T13:32:32+00:00

I work on implementation of playing card class and i’ve created the following method:

  • 0

I work on implementation of playing card class and i’ve created the following method:

+ (id)cardWithCard:(Card *)newCard
{
Card *card = [[[Card alloc] initWithCard:newCard] autorelease];
return card;
}

i use autorelease method here cause otherwise Product->Analyze warns me about potential leak. Everything works fine until variable myCard (which was allocated earlier) to which a new value was assigned like this:myCard = [Card cardWithCard:newCard] is sent to a different method as a parameter. It turns out to be deallocated there and my app crashes.
How should i resolve this issue? Get autorelease method away despite warnings of Analyze?

  • 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-27T13:32:33+00:00Added an answer on May 27, 2026 at 1:32 pm

    Each method or object should be responsible for retaining object that it is interested in.
    It it easier to maintain. (Exception are for method that have alloc, new or copy in there name, they will return an object that the caller is responsible to release.)

    So you need to keep the autorelease because that method don’t exist after the return and won’t be able to call release on it.
    The Object that is calling cardWithCard should retain the object if it want it to be alive more that the time of that particular method.

    The code should look something like this

    self.myCard = [Card cardWithCard:newCard];
    

    This is in the case where myCard is declare like this

    @property (nonatomic, retain) Card * myCard;
    

    So in this case the property will do the retain for you and will release that object when you will place a new one in this property. (If you overwrite the auto-generated accessor, you will need to manage that yourself in those method)

    If for some reason you don’t want to use a property… well it’s your choice 🙂
    You will need to do something like this:

    myCard = [[Card cardWithCard:newCard] retain];
    

    and you will need to something like this later on

    [myCard release];
    

    If it’s not in the same method the analyser will compline, and if it’s in the same method you probably don’t need the retain.

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

Sidebar

Related Questions

The implementation used where I work uses about 10 columns in the table: CUSTOMFIELD1,
Is there a message queue implementation that allows breaking up work into 'batches' by
In our data access layer at work we have this standard implementation where the
I'm having some difficulties getting the TabActivity to work. Here's the implementation of the
Work on a support helpdesk. New tickets come in and records are created in
Work on this small test application to learn threading/locking. I have the following code,
I want to have an abstract class Server with an abstract method called Initialize
Before start work on implementation Model layout, I ask few question on this site:
For fun, I'm playing with a class to easily cache function results. The basic
I tried to understand iterator implementation, and while playing around with the source, I

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.