Question

I found out, that every time I change the view the new instance of them will be created. (Memory increase every change of the view). I would like to dealloc the actually view, if I go the the previous one. This can not be done by dealloc, because I am using ARC. link between the views

The "BACK"-Button is just linked to the Config Menu. At the time I need to dealloc the error view to create the new instance next time.
The init of the Error view looks like:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

The other buttons of the config menu are also just linked to the next view. So I do not create the instance of them manually. If you need some other parts of code, I will put it in.

MORE CODE:

Config View:(almost empty, only 3 linked buttons)

#import "ConfigMenuViewController.h"

@interface ConfigMenu ()

@end

@implementation ConfigMenu

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

@end

ErrorView.m:

#import "Error.h"
#import "CANlinkWLAN.h"
#import "SocketConnectionControllerThread.h"

@interface Error ()

@end

@implementation Error
@synthesize canBufferOverflow;
@synthesize canTransmitTimeout;
@synthesize canErrorcounterOverflow;
@synthesize canBusOffError;
@synthesize usbtors232SyntexError;
@synthesize usbtors232FormatError;
@synthesize usbtors232BufferOverflow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter ]addObserver:self selector:@selector(receivedMessage:) name:@"GET_ERROR_STATUS" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)readError:(id)sender {
    [[SocketConnectionControllerThread sharedInstance] sendCommand: GET_ERROR_STATUS];
}

- (void) receivedMessage: (NSNotification *) note{
    NSDictionary *userInfo = [note userInfo];
    unsigned char error = [(NSNumber *)[userInfo objectForKey:@"GET_ERROR_STATUS"] integerValue];
    NSLog(@"Error: %d", (int)error);
    [self receivedError: &error];
}

- (void) receivedError:(unsigned char *) msg{

    //change some colors ( not important) ; 
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"GET_ERROR_STATUS" object:nil];
}

@end

ErrorView.h

#import "ViewController.h"

@interface System : ViewController
{
    UILabel *serverEcho;
    UILabel *messageEcho;
    UILabel *usbtors232Output;
}

- (IBAction)reset:(id)sender;
- (IBAction)setAutobaud:(id)sender;
- (IBAction)readFeedback:(id)sender;
- (void) receivedMessage: (NSNotification *) note;
@property (nonatomic, retain) IBOutlet UILabel *usbtors232Output;
@property (nonatomic, retain) IBOutlet UILabel *messageEcho;
@property (nonatomic, retain) IBOutlet UILabel *serverEcho;

@end
Was it helpful?

Solution 2

I found the way. It was much easier as I expected.

- (IBAction)goBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

That way I really go back to the previous view and release the allocated memory. + enter image description here

OTHER TIPS

What happens with ARC is that there's a counter automatically incremented of all of the strong pointers to your object. If you set to nil all the strong pointers to it, ARC will automatically dealloc the memory allocated for the object. What you can do is implement the dealloc method in you ErrorView and log something. Then see if it's deallocated. Simply set to nil all pointers pointing to this object and the memory will be freed.

Edit more code :

I think your dealloc method is never called since you add yourself as an observer of UINotification and you remove yourself from it in the dealloc method (which is never called) so it creates a "memory loop" that you never get out of. Try calling

[[NSNotificationCenter defaultCenter]removeObserver:self name:@"GET_ERROR_STATUS" object:nil];

when you press the back button for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top