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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:11:35+00:00 2026-05-30T09:11:35+00:00

I have read the posts I found here and in many other places –

  • 0

I have read the posts I found here and in many other places – no answers.

I have a simple class that contains NSString:

MyItem.h

#import <Foundation/Foundation.h>
@interface MyItem : NSObject 
{
NSString * ItemName;
NSNumber * TestID;
NSMutableArray * Items;
}

//property
@property (nonatomic,retain) NSString * ItemName;
@property (nonatomic,retain) NSNumber * TestID;
@property (nonatomic,retain) NSMutableArray * Items;

// methods
-(id)initWithName:(NSString*)theName;
@end

MyItem.M

#import "MyItem.h"
@implementation MyItem
@synthesize Items;
@synthesize TestID;
@synthesize ItemName;
-(id)initWithName:(NSString*)theName
{
    ItemName=theName;
    Items=[[NSMutableArray alloc]init];
    return self;
}
@end

It is very simple, as the class is created, the name is retained and the array allocated.
In order to have view controllers sharing this class, I have created this protocol:

MasterPager.h

#import <Foundation/Foundation.h>

@class MyItem; 

@protocol MasterPager <NSObject>
@required
@property (nonatomic,retain) MyItem * currentItem;
-(void)dumpItems;
@end

which I then use in my appdelegate:

ArrayTestAppDelegate.h

#import <UIKit/UIKit.h>
#import "MasterPager.h"

@class ArrayTestViewController;

@interface ArrayTestAppDelegate : NSObject <UIApplicationDelegate,MasterPager> 
{
      //MyItem * currentItem;
}
@property (nonatomic,retain) MyItem * currentItem;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ArrayTestViewController *viewController;

@end 

I’m instanciating this property in the application didFinishLaunchingWithOptions as so:

@synthesize currentItem;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:            (NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    currentItem=[[MyItem alloc] initWithName:@"main stuff"];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    self.viewController.mainPage=self;
    return YES;
}

and here is the dumpItem method:

-(void)dumpItems
{
    NSLog(@"Dumping %@",currentItem.ItemName);
    for(int i=[currentItem.Items count]-1;i>=0;i--)
    {
        MyItem * item=[currentItem.Items objectAtIndex:i];
        NSLog(@"current item id:%@", item.TestID );
        NSLog(@"current item name:%@", item.ItemName );
    }
}

(Sorry for all this text, but it is probably required).

Now, I have a view controller that I use in order to test this.
This view controller has 2 buttons, each of them triggers different function.
the first function to create some (4) sub items in this object is working fine:

-(IBAction)onCreate:(id)sender
{
    for(int i=0;i<4;i++)
    {
        MyItem * item=[[MyItem alloc] initWithName :[NSString stringWithFormat:@"Test number %d",i]];
        item.TestID=[NSNumber numberWithInt:i];
        [mainPage.currentItem.Items addObject:item];
    }

    [mainPage dumpItems];
}

As you can see the dumpItems is called and it does what its suppose to do, dumping the objects.

********NOW… here is the thing!*************

There is a second button, as mentioned, that execute the same function:

- (IBAction)onDump:(id)sender
{
    NSLog(@"executing dump on the protocol");
    [mainPage dumpItems];
}

After creation, clicking the second button is calling this method which in turn calls the same dumpItems! BUT, when this is executed, an exc_bad_access is thrown when the line

NSLog(@"current item name:%@", item.ItemName );

is reached. comment the line and it’s all working.
un-commenting the //MyItem * currentItem; will do nothing. So, how could it be?
NSZombieEnabled ? Tried that, did nothing.
There is no release call in sight, and if there were, how come the NSNumber dump working just fine?
Also, nothing happen between the first button clicked and the second one.
but still, the strings somehow disappears!

  • 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-30T09:11:36+00:00Added an answer on May 30, 2026 at 9:11 am

    is this ARC? If not, it’s not that hard, and not that cool 😉

    You pass an autoreleased NSString to your init method

        MyItem * item=[[MyItem alloc] initWithName :[NSString stringWithFormat:@"Test number %d",i]];
        //                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ autoreleased object 
    

    Unfortunately you don’t retain that autoreleased string in your init.

    -(id)initWithName:(NSString*)theName {
        ItemName=theName;    // <- no retain
    }
    

    the code steps out of the init method and you run dumpItems on the newly created object

        [mainPage dumpItems]; // <- within same runloop iteration. AutoreleasePool has not be drained. 
    

    since you call dumpItems before the end of the current runloop the autoreleased object still exists.

    But the IBAction method happens after the autoreleased object has been deallocated (the object was deallocated when the autorelease pool was drained at the end of the current runloop).

        - (IBAction)onDump:(id)sender
        {
            NSLog(@"executing dump on the protocol");
            [mainPage dumpItems]; // <- not in the same runloop. AutoreleasePool has been drained. Autoreleased object has been deallocated
        }
    

    the fix:

    -(id)initWithName:(NSString *)theName
    {
        if ((self = [super init])) {
            itemName = [theName retain];            // to match your @property
            items = [[NSMutableArray alloc] init];
        }
        return self;
    }
    

    By the objective-c code style guidelines only Class names (e.g. NSString, MyItem) should start with a capital letter. You should fix this to improve readability (and the code formatting on stackoverflow)

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

Sidebar

Related Questions

I have read some posts on here about not mixing parameters when passing into
I have read some posts about this topic and the answers are comet, reverse
I have read the other posts, e.g., vim: Executing a list of editor commands
Alright...I've given the site a fair search and have read over many posts about
I have read in some of the ClickOnce posts that ClickOnce does not allow
Okay guys, I have read through all the other posts and question on jQuery
I have found many questions here about storing values in viewstate, but haven't found
I have read through all relevant posts on Prawn but found no mentioning (even
I have read the other posts about the notorious _imaging C module error when
I have read posts like these: What is a metaclass in Python? What are

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.