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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:50:35+00:00 2026-05-14T18:50:35+00:00

i’m a newbie to iphone development. I’m doing a navigation-based app, and I’m having

  • 0

i’m a newbie to iphone development. I’m doing a navigation-based app, and I’m having trouble passing values to a new view.

@interface RootViewController : UITableViewController {
    NSString *imgurl;
    NSMutableArray *galleryArray;
}

@property (nonatomic, retain) NSString *imgurl;
@property (nonatomic, retain) NSMutableArray *galleryArray;

- (void)showAll;

@end

#import "RootViewController.h"
#import "ScrollView.h"
#import "Model.h"
#import "JSON/JSON.h"

@implementation RootViewController

@synthesize galleryArray, imgurl;

- (void)viewDidLoad {   
    UIBarButtonItem *showButton = [[[UIBarButtonItem alloc]
                                initWithTitle:NSLocalizedString(@"Show All", @"")
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(showAll)] autorelease];
    self.navigationItem.rightBarButtonItem = showButton;

    NSString *jsonString = [[Model sharedInstance] jsonFromURLString:@"http://www.ddbstaging.com/gerald/gallery.php"];
    NSDictionary *resultDictionary = [jsonString JSONValue];

    if (resultDictionary == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Webservice Down" 
                                                    message:@"The webservice you are accessing is currently down. Please try again later." 
                                                   delegate:self 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else {
        galleryArray = [[NSMutableArray alloc] init];
        galleryArray = [resultDictionary valueForKey:@"gallery"];
        imgurl = (NSString *)[galleryArray objectAtIndex:0];
        NSLog(@" -> %@", galleryArray);
        NSLog(imgurl);
    }
}

- (void)showAll {
    NSLog(@" -> %@", galleryArray);
    NSLog(imgurl);
    ScrollView *controller = [[ScrollView alloc] initWithJSON:galleryArray];
    [self.navigationController pushViewController:controller animated:YES];
}

The RootViewController startup and the json data loads up fine. I can see it from the first console trace. However, once I click on the Show All button, the app crashes. It doesn’t even trace the galleryArray and imgurl properyly.

Maybe additional pairs of eyes can spot my mistakes. Any help is greatly appreciated!

[Session started at 2010-05-08 16:16:07 +0800.]
2010-05-08 16:16:07.242 Photos[5892:20b]  -> (
)
GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 5892.
(gdb) 
  • 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-14T18:50:36+00:00Added an answer on May 14, 2026 at 6:50 pm

    (1) Try using

    NSLog(@"%@", imgurl);
    

    The 1st argument of NSLog is always interpreted as a format string, even if it’s a variable (the runtime doesn’t care). If imgurl contains format specifiers (e.g. %2c) then something undefined may happen.


    (2)

        galleryArray = [[NSMutableArray alloc] init];
        galleryArray = [resultDictionary valueForKey:@"gallery"];
        imgurl = (NSString *)[galleryArray objectAtIndex:0];
        NSLog(@" -> %@", galleryArray);
        NSLog(imgurl);
    

    There are 2 problems here. The 2nd line will override the empty mutable array initialized in the 1st line, thus causing a memory leak. The galleryArray assigned in the 2nd line will likely be released after the function ends, thus creating a dangling pointer. The same goes for imgurl. This probably is the main cause of the crash. I recommend you read the Cocoa Memory Management Rules again. Anyway, this piece of code should be like:

        self.galleryArray = [resultDictionary valueForKey:@"gallery"];
        self.imgurl = [galleryArray objectAtIndex:0];
        NSLog(@" -> %@", galleryArray);
        NSLog(@"%@", imgurl);
    

    or

        [galleryArray release];
        galleryArray = [[resultDictionary valueForKey:@"gallery"] retain];
        [imgurl release];
        imgurl = [[galleryArray objectAtIndex:0] retain]; // or -copy.
        NSLog(@" -> %@", galleryArray);
        NSLog(@"%@", imgurl);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.