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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:15:25+00:00 2026-05-23T18:15:25+00:00

A lot of my memory leaks are coming from this code that recognizes swipes.

  • 0

A lot of my memory leaks are coming from this code that recognizes swipes. What am I doing wrong? The first line is something that I think is leaking (using Instruments). It’s being shown as the responsible caller for a lot of the errors
This is in ViewDidLoad:

   UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
    [(UISwipeGestureRecognizer *)swipeRight setNumberOfTouchesRequired:2];

    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    swipeRight.delegate = self;
    [webView addGestureRecognizer:swipeRight];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
    [(UISwipeGestureRecognizer *)swipeLeft setNumberOfTouchesRequired:2];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeLeft.delegate = self;
    [webView addGestureRecognizer:swipeLeft];

    // Do any additional setup after loading the view from its nib.

}

One more question, what could cause a zombie here? Should I be autoreleasing?

    AViewController *a = [[AViewController alloc]init];
[self.navigationController pushViewController:a animated:YES];

a.title =@"A View";
[a release];

Update 3: I ran instruments to look for bad allocations and with some intensive use I get a zombie here:
Error Message: An Objective-C message was sent to a deallocated object (zombie) at address: 0xf583270.
In instruments here’s what I’m seeing. Instruments highlights this line and has 100% next to it.

AViewController *a = [[AViewController alloc]init];
  • 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-23T18:15:25+00:00Added an answer on May 23, 2026 at 6:15 pm

    Memory management in object-c is something that takes a bit of getting used too for sure. I personally let the operating handle everything for me. What this means is everytime I alloc something, I just give it an autorelease. The OS will handle the release for me when it needs to. The only time this is a problem is when you reuse an object in the same scope, the OS will send it too many releases and free the memory before you want it too. Here is an example

    //This code will result in a memory crash
    CustomObject *coolThing = [[[CustomObject alloc] init] autorelease];
    [coolThing setAwesomeLevel:10];
    [array addObject:coolThing];
    
    [coolThing setAwesomeLevel:7];
    [array2 addObject:coolThing];
    

    Instead you would use

    //Working code
    CustomObject *coolThing = [[CustomObject alloc] init];
    [coolThing setAwesomeLevel:10];
    [array addObject:coolThing];
    
    [coolThing setAwesomeLevel:7];
    [array2 addObject:coolThing];
    
    [coolThing release];
    

    Now, to use autoreleases with your code, all you would do is add them to the allocs. This is why your code is leaking. When you add it to the webView object, it is increasing its retain account. And when you leave that scope, it has a retain account of 2, but you only ever send it one release (its retain would remain at 1, and never release the memory).

    UISwipeGestureRecognizer *swipeRight = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)] autorelease];
    [(UISwipeGestureRecognizer *)swipeRight setNumberOfTouchesRequired:2];
    
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    swipeRight.delegate = self;
    [webView addGestureRecognizer:swipeRight];
    
    UISwipeGestureRecognizer *swipeLeft = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)] autorelease];
    [(UISwipeGestureRecognizer *)swipeLeft setNumberOfTouchesRequired:2];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeLeft.delegate = self;
    [webView addGestureRecognizer:swipeLeft];
    

    if you do not want to use autorelease like this, you need to simply add some releases after you add the gestures to the webView.

    [swipeRight release];
    [swipeLeft release]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If an application produces a lot of memory leaks, are they just an in-app
I have a program that needs a lot of memory and want to set
I have a python program that is going to eat a lot of memory,
I wonder if this affects performance or memory consumption a lot. I need an
A lot of time I have to code from home. Normally I just remote
I have been solving a lot of memory leaks but have been unsuccessful in
is there a way to detect simple memory leaks like this one with a
I have already found a lot of information about how to solve memory leaks
_CrtDumpMemoryLeaks(); if you didn't know, is a function that dumps all the memory leaks
Hi I'm usuing [NSThread detachNewThreadSelector:toTarget:withObject:] and I'm getting a lot of memory leaks because

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.