Problem with SIngleton Class
        Posted  
        
            by zp26
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by zp26
        
        
        
        Published on 2010-06-14T10:13:51Z
        Indexed on 
            2010/06/14
            10:22 UTC
        
        
        Read the original article
        Hit count: 201
        
Hi, I have a prblem with my code. I have a viewController and a singleton class. When i call the method readXml and run a for my program update the UITextView. When i call the clearTextView method the program exit with EXC_BAD_ACCESS. The prblem it's the name of the variable position. This is invalid but i don't change anything between the two methods. You have an idea? My code:
#import "PositionIdentifierViewController.h"
#import "WriterXML.h"
#import "ParserXML.h"
#define timeToScan 0.1
@implementation PositionIdentifierViewController
@synthesize accelerometer;
@synthesize actualPosition;
@synthesize actualX;
@synthesize actualY;
@synthesize actualZ;
-(void)updateTextView:(NSString*)nomePosizione
{
    NSString *string = [NSString stringWithFormat:@"%@",nomePosizione];
    textEvent.text = [textEvent.text        stringByAppendingString:@"\n"];
    textEvent.text = [textEvent.text        stringByAppendingString:string];
}
-(IBAction)clearTextEvent{
 textEvent.text = @"";
 //with this for my program exit
 for(int i=0; i<[[sharedController arrayPosition]count]; i++){
  NSLog(@"sononelfor");
  Position *tempPosition = [[Position alloc]init];
  tempPosition = [[sharedController arrayPosition]objectAtIndex:i];
  [self updateTextView:(NSString*)[tempPosition  name]];
 }
}
-(void)readXml{
    if([sharedController readXml]){
        UIAlertView *alert = [[UIAlertView  alloc] initWithTitle:@"Caricamento Posizioni" message:@"Caricamento effettuato con successo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert  show];
        [alert  release];   
        NSString *string = [NSString    stringWithFormat:@"%d", [[sharedController arrayPosition]count]];
        [self   updateTextView:(NSString*)string];
        //with only this the program is ok
        for(int i=0; i<[[sharedController   arrayPosition]count]; i++){
            NSLog(@"sononelfor");
            Position *tempPosition = [[Position alloc]init];
            tempPosition = [[sharedController arrayPosition]objectAtIndex:i];
            [self   updateTextView:(NSString*)[tempPosition     name]];
        }
    }
    else{
        UIAlertView *alert = [[UIAlertView  alloc] initWithTitle:@"Caricamento Posizioni" message:@"Caricamento non riuscito" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert  show];
        [alert  release];   
    }
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    sharedController = [SingletonController sharedSingletonController];
    actualPosition = [[Position alloc]init];
    self.accelerometer = [UIAccelerometer sharedAccelerometer];
    self.accelerometer.updateInterval = timeToScan;
    self.accelerometer.delegate = self; 
    actualX=0;
    actualY=0;
    actualZ=0;
    [self   readXml];
}
- (void)dealloc {
    [super dealloc];
    [actualPosition dealloc];
    [super dealloc];
}
@end
#import "SingletonController.h"
#import "Position.h"
#import "WriterXML.h"
#import "ParserXML.h"
#define standardSensibility 2
#define timeToScan .1
@implementation SingletonController
@synthesize arrayPosition;
@synthesize arrayMovement;
@synthesize actualPosition;
@synthesize actualMove;
@synthesize stopThread;
+(SingletonController*)sharedSingletonController{
    static SingletonController *sharedSingletonController;
    @synchronized(self) {
        if(!sharedSingletonController){
            sharedSingletonController = [[SingletonController alloc]init];
        }
    }
    return sharedSingletonController;
}
-(BOOL)readXml{
    ParserXML *newParser = [[ParserXML alloc]init];
    if([newParser   startParsing:(NSString*)@"filePosizioni.xml"]){
        [arrayPosition addObjectsFromArray:[newParser arrayPosition]];
        return TRUE;
    }
    else
        return FALSE;
}
-(id)init{
    self = [super init];
    if (self != nil) {
        arrayPosition = [[NSMutableArray alloc]init];
        arrayMovement = [[NSMutableArray alloc]init];
        actualPosition = [[Position alloc]init];
        actualMove = [[Movement alloc]init];
        stopThread = FALSE;
    }
    return self;
}
-(void) dealloc {
    [super dealloc];
}
@end
© Stack Overflow or respective owner