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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:52:06+00:00 2026-05-13T05:52:06+00:00

As you can see in the code below I am creating and initialising a

  • 0

As you can see in the code below I am creating and initialising a Vertex which I then add into a NSMutableArray instance variable within my Frame object. As I currently have this setup myVert is owned by main and pointed to by vertexList. Would I be better setting this up so that I make a copy of inVertex within the addVertex method so the object takes ownership of its data?

-(void)addVertex:(Vertex*) inVertex {
    [vertexList addObject:inVertex];
}

// -------------------

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Frame *myFrame;
    Vertex *myVert;

    NSLog(@"MDD_ObjectTest ... ");
    myFrame = [[Frame alloc] init];
    myVert = [[Vertex alloc] initWithxPos:111 yPos:222 zPos:333];

    [myFrame addVertex:myVert];
    [myVert release];

    // Clean up
    [myFrame release];
    [pool drain];
    return 0;
}

Finally if I should make a copy what is the best way to do that, should I be looking at …

-(void)addVertex:(Vertex*) inVertex {
    [vertexList addObject:[inVertex copy]];
}

Although I am a little unsure what to do in terms of the Vertex object with regards to copyWith Zone.

gary

  • 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-13T05:52:06+00:00Added an answer on May 13, 2026 at 5:52 am

    This depends on if Vertex is mutable or not.

    If Vertex is immutable:

    Since the inVertex can not have any of its data changed, the current way you have it is fine. The vertexList will retain inVertex when you add it and will release it when you remove it. Since the properties of inVertex can not be changed there is no difference in storing it versus a copy.

    If Vertex is mutable:

    You should store a copy so that the changes to inVertex do not affect the vertex stored in the list. Note however that you have a possible memory leak in the way you have it right now. When you copy an object that copy’s retain count is set to 1, then when you store it in the vertexList the retain count becomes 2. When you remove it from the list it will have a retain count of 1, causing a memory leak unless you remember to release it. The best place to release it would be in the addVertex method after it is added to the vertexList to keep its retain count at 1, since only one thing has a reference to it.

    - (void) addVertex:(Vertex *) inVertex {
        Vertex * copy = [inVertex copy];
        [vertexList addObject:copy];
        [copy release];
    }
    

    Note that Vertex must implement the NSCopying protocol for this to work.

    I suggest using the immutable approach unless you have a real valid reason to make vertex mutable other than convenience.

    Edit: Regarding copyWithZone:

    To implement object copying you have to implement the NSCopying protcol that defines the copyWithZone: method. The only consideration you need to make with the zone is that instead of calling the alloc method on the Vertex class you call the allocWithZone: method instead.

    - (id) copyWithZone:(NSZone *) zone {
        Vertex * copy = [[Vertex allocWithZone:zone] initWithxPos:x yPos:y zPos:z];
        // Any other copying that needs to be done
        return copy;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 355k
  • Answers 356k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could do it with: $ git branch task1 #… May 14, 2026 at 8:46 am
  • Editorial Team
    Editorial Team added an answer The following content was originally taken from MSDN http://xbox.create.msdn.com/downloads/?id=123&filename=DataStructures_CheatSheet.doc (but… May 14, 2026 at 8:46 am
  • Editorial Team
    Editorial Team added an answer Change "mysite.com" to be ".mysite.com" (note the leading "."). That'll… May 14, 2026 at 8:46 am

Related Questions

I'm using System.Data.Design.TypedDataSetGenerator to convert a .xsd file (generated by VS2008) into a strongly-typed
I am a little confused about how I set and get the instance variables
I need to write an app that that will iterate over our database, and
Strange programming problems as of now..As you can see below i have assigned intFrontPtr
I am trying to disable a number of text boxes intended for displaying data

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.