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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:08:44+00:00 2026-05-13T23:08:44+00:00

I am leaking memory on this: my custom class: + (id)vectorWithX:(float)dimx Y:(float)dimy{ return [[[Vector

  • 0

I am leaking memory on this:

my custom class:

+ (id)vectorWithX:(float)dimx Y:(float)dimy{
return [[[Vector alloc] initVectorWithX:dimx Y:dimy] autorelease]; }


- (Vector*)add:(Vector*)q {
return [[[Vector vectorWithX:x+q.x Y:y+q.y] retain] autorelease]; }

in app delegate I initiate it:

Vector *v1 = [[Vector alloc] initVector];
Vector *v2 = [[Vector alloc] initVector];       
Vector *vtotal = [[v1 add:v2] retain];

[v1 release];
[v2 release];
[vtotal release];

How this leaks? I release or autorelease them properly. The app crashes immediately if I don’t retain these, because of an early release I guess. It also crashes if I add another release.

  • 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-13T23:08:45+00:00Added an answer on May 13, 2026 at 11:08 pm

    Why do you think you’re leaking memory? Start with that. And what object do you crash on accessing? That will tell you most likely what object your under-retain is related to. If I had to make a guess, I’d suspect initVector, just because it’s a very strange name for a method. What does it do? Why is it not called just “init?”

    Is this threaded code? You’re doing a lot more retain/autorelease than is generally appropriate. You do not need to retain an object in order to hold onto it through the current event loop. Generally speaking you only retain ivars, because those are what you need through the next event loop. If you have a lot of calls to retain outside of accessors, then you’re almost certainly mis-managing memory. The above should be:

    + (id)vectorWithX:(float)dimx y:(float)dimy
    {
        return [[[Vector alloc] initVectorWithX:dimx y:dimy] autorelease];
    }
    
    - (Vector*)add:(Vector*)q
    {
        return [Vector vectorWithX:(self.x + q.x) y:(self.y + q.y)];
    }
    
    ...
    
    Vector *v1 = [[Vector alloc] initVector];
    Vector *v2 = [[Vector alloc] initVector];
    Vector *vtotal = [v1 add:v2];
    ...
    [v1 release];
    [v2 release];
    

    Personally, I would handle v1/v2 with autorelease because I think it makes the code more maintainable and understandable, but there are other schools of thought:

    Vector *v1 = [[[Vector alloc] initVector] autorelease];
    Vector *v2 = [[[Vector alloc] initVector] autorelease];
    Vector *vtotal = [v1 add:v2];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 358k
  • Answers 358k
  • 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 Most of the times, search engines deal with latin counterparts… May 14, 2026 at 2:14 pm
  • Editorial Team
    Editorial Team added an answer The technology that best suits you would be WCF RIA… May 14, 2026 at 2:14 pm
  • Editorial Team
    Editorial Team added an answer I think you're looking for the XmlAttribute attribute: <Serializable()> Public… May 14, 2026 at 2:14 pm

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.