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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:14:15+00:00 2026-06-03T18:14:15+00:00

I have the next piece of code, one iVar with this property retained and

  • 0

I have the next piece of code, one iVar with this property retained and released in it’s class dealloc method. The iVar is used in 2 methods and continually change the value but
sometimes when I use the value is corrupted. Why is that?

.h

@interface ChatController : NSObject <ASIHTTPRequestDelegate>{
NSTimer *timer;
NSString *_idLastMessageFromServer;
}

@property(nonatomic, retain)NSString *idLastMessageFromServer;
@end

.m

@implementation ChatController

@synthesize idLastMessageFromServer = _idLastMessageFromServer;

- (void)initLoopTimer{
timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(update:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)update:(id)sender{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:CONSTANT_YYYY];
[request setDelegate:self];
[request addPostValue:_idLastMessageFromServer forKey:CONSTANT_XXX];
[request setDidFinishSelector:@selector(requestUpdateFinish:)];
[request startAsynchronous];
}

- (void)requestUpdateFinish:(ASIHTTPRequest *)request{
NSString *response = [request responseString];
if(response && response.length){
    if(![response isEqualToString:CHAT_RESPONSE_NO_MESSAGES]){
        NSArray *array = [response componentsSeparatedByString:CHAT_PARSE_RESPONSE];
        if(array && [array count] == 2){
            **_idLastMessageFromServer = [array objectAtIndex:0];**
        }
     }
   }
}

But when the loop calls the method update:, it crashes in this line of code

[request addPostValue:_idLastMessageFromServer forKey:CONSTANT_XXX];

with EXC_BAD_ACCESS message, but why?

  • 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-03T18:14:19+00:00Added an answer on June 3, 2026 at 6:14 pm

    By using _idLastMessageFromServer instead of self.idLastMessageFromServer, you are not retaining the string. The allows the retain count to drop to zero which deallocates the object. At that point you have a reference to bad memory, hence the app crashes.

    Don’t use iVars directly unless you have a good reason (like -init or -dealloc). Use the property instead.

     [request addPostValue:self.idLastMessageFromServer forKey:CONSTANT_XXX];
    

    and

    self.idLastMessageFromServer = [array objectAtIndex:0];
    

    I’ll add a bit more of a detailed explanation about properties.

    self.idLastMessageFromServer when used to read the value of the property calls an auto generated method -idLastMessageFromServer. This method will look something like:

    - (NSString *)idLastMessageFromServer
    {
        return _idLastMessageFromServer;
    }
    

    self.idLastMessageFromServer when used to set the value of the property calls an auto generated method -setIdLastMessageFromServer:. This method will look something like:

    - (void)setIdLastMessageFromServer:(NSString *)idLastMessageFromServer
    {
        if (_idLastMessageFromServer != idLastMessageFromServer) {
            [_idLastMessageFromServer release];
            _idLastMessageFromServer = idLastMessageFromServer;
            [_idLastMessageFromServer retain];
        }
    }
    

    One final note: be sure to release _idLastMessageFromServer in your -dealloc method. Something like:

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

    More details about properties and iVars.

    Properties (like self.idLastMessageFromServer) are simply a easy way to handle getter and setter methods. They cannot hold data because they are methods. iVars (like _idLastMessageFromServer) are a pointer to a location in memory. They cannot control access and maintain state because they are simply a pointer.

    Properties and iVars work together.

    The line @property(nonatomic, retain) NSString *idLastMessageFromServer; says that somewhere in my implementation, my class will have a getter and setter for the property idLastMessageFromServer.

    The line @synthesize idLastMessageFromServer = _idLastMessageFromServer; auto generates the getter and setter methods for idLastMessageFromServer using the iVar _idLastMessageFromServer.

    In short, the property controls access to the iVar; the iVar is the storage location for the property.

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

Sidebar

Related Questions

I have next code require 'rack/rpc' class Server < Rack::RPC::Server def hello_world Hello, world!
I have one piece of Cocoa code I wrote that takes in an XML
I have written this piece of code here and I have linked it alright
I have C# code that creates HTML. One particular piece of code creates a
I have this simple piece of code: HTML: <body> <div id=red> ABC </div> <div
I have this piece of code: $('#contentSites').fadeOut(200, function() { // Animation complete $.get(my_account_content.php?q= +
I have this piece of code that originally uses the replace function(VBA) to replace
I have the next piece of code: $(#slider-cpu).slider({ range: min, value: 0, min: 0,
I have this small piece of code that simply pulls in a news release
I have this piece of code for (element <- table.find; Right(info) = exceptionManager(mapper.getInfoFromDbObject(element))) yield

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.