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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:02:51+00:00 2026-06-06T06:02:51+00:00

I want to test the block function and the value for the context. The

  • 0

I want to test the block function and the value for the context. The test code is:

//xxx.h

@interface textObj :  NSObject 
@property (nonatomic, retain) NSNumber * num;
@end

typedef void (^ returnHandle)(NSNumber * res);

@interface BlockTest : NSObject

- (void) textBlock:(textObj *)num completionHandler:(void (^)(NSNumber * res))handler;

@end

//xxx.m

@implementation textObj
@synthesize num;

@end

@interface BlockTest (){
    returnHandle rt;
}

- (void)toggleChromeDisplay;

@end

@implementation BlockTest

- (void) dealloc{
    Block_release(rt);
    [super dealloc];
}

- (void)toggleChromeDisplay
{
    NSNumber *ret = [NSNumber numberWithInt:111];
    rt(ret);
}

void (^handle)(NSNumber * res, NSError *error);


- (void) textBlock:(textObj *)g1 completionHandler:(void (^)(NSNumber * res))handler
{
    rt = Block_copy(handler);
    [self performSelector:@selector(toggleChromeDisplay) withObject:nil afterDelay:0.5];
    return;
}

@end

The test sample invoke code is :

//first sample code...
- (void)viewDidLoad
{
    [super viewDidLoad];

    test = [[BlockTest alloc]init];
    textObj * g1;
    g1 = [[textObj alloc] init];
    [g1 setNum:[NSNumber numberWithInt:10]];
    NSLog(@"main 0 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);
    [test textBlock:g1 completionHandler:^(NSNumber *res) {
        NSLog(@"value=%@", [g1 num]);
        [g1 setNum:[NSNumber numberWithInt:20]];
        NSLog(@"main 1 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);
    } ];
    NSLog(@"main 2 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);
    [g1 release];
    NSLog(@"main 3 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);  
}

In the block function, i can change the g1 value. It is OK.
But when I declare the g1 as the public value for the test, it will error in the block function. The g1 can not be access. It output the EXC_BAD_ACCESS(code=2, address=0x26) error.

//second sample code...
@interface UIMainViewController (){
@public
    textObj * g1;
}
@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    test = [[BlockTest alloc]init];
    //textObj * g1;
    g1 = [[textObj alloc] init];
    [g1 setNum:[NSNumber numberWithInt:10]];
    NSLog(@"main 0 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);
    [test textBlock:g1 completionHandler:^(NSNumber *res) {
        NSLog(@"value=%@", [g1 num]);
        [g1 setNum:[NSNumber numberWithInt:20]];
        NSLog(@"main 1 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);
    } ];
    NSLog(@"main 2 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);
    [g1 release];
    NSLog(@"main 3 g1 num=%@ count=%d", [g1 num], [g1 retainCount]);  
}

I am confuse the two different sampe code, why the second test code will meet crash?

  • 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-06T06:02:54+00:00Added an answer on June 6, 2026 at 6:02 am

    The primary problem is that, as @H2CO3 points out, you’re releasing g1 before the block is executed. The reason it works in one case and not the other is that blocks retain any local object variables they refer to when copied.

    In your first example, g1 is a variable local to the method scope, so the block retains it.

    In your second example, g1 is an ivar (effectively self->g1), so the block retains self. But you release g1 right after you declare the block, so when the block calls self->g1, it gets an invalid pointer, because g1 has been deallocated.

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

Sidebar

Related Questions

I want to test my part of code that returns the users password question.
I have the following: var test = dojo.byId(clientDivision); test.style.display = 'block'; in a function
I want test a FIX gateway for our company and was wondering if anything
I want to test whether a certain string is contained in a short list
I want to test my app for my app launched with a URL scheme
I'd like to block users from typing certain characters in a text box (want
<div onclick="test(this)"> Test <div id="child">child</div> </div> I want to change the style of the
I have a model function that I want to make sure uses a transaction.
Hi I need to implement a function that if the value of the binding
I have a function (internally uses ASIHTTPRequest) which calls a block with the result:

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.