Potential Leak of an object allocated - objective-c

+ (WAController*) sharedWAController {
#synchronized([WAController class]) {
if (!_sharedWAController)
[[self alloc] init];
return _sharedWAController;
}
}
This is show in potential leak
for [[self alloc] init];
Why its leak ?

You never assign it to anything so it will just allocate a new object and leak it. To fix the leak and the incorrectly working code assign _sharedWAController
if (!_sharedWAController)
_sharedWAController = [[self alloc] init];

make it : _sharedWAController = [[self alloc] init];

+ (WAController*) sharedWAController {
#synchronized([WAController class]) {
if (!_sharedWAController)
{
_sharedWAController = [[self alloc] init];
}
return _sharedWAController;
}
}

+ (CommonUtility*) sharedUtility {
#synchronized([CommonUtility class]) {
if (!_sharedUtility)
_sharedUtility = [[self alloc] init];
return _sharedUtility;
}
}
+ (id) alloc {
#synchronized([CommonUtility class]) {
_sharedUtility = [super alloc];
return _sharedUtility;
}
}
I try like this is this perfect. But I allocated _sharedUtility object where i need to release. this In dealloc or autorelesae it.

Related

How to override init method to return other class?

How to override init method to return other class? Like this
ClassA.m
- (id)initWithType:(int)type{
if(type == 0){
return [[ClassB alloc] init];
}
else if(type == 1){
return [[ClassC alloc] init];
}
return [self init];
}
id classInstance = [[ClassA alloc] initWithType:someType];
So, in ARC, if I should care about the alloc operation of ClassA?
And, in NonARC, what's the difference?
BTW, Using class method to implement is not in discussion, I only care about how to override init method to return other class?
your code looks fine with ARC. for non-ARC, you need to release self
- (id)initWithType:(int)type{
if(type == 0){
[self release]; self = nil;
return [[ClassB alloc] init];
}
else if(type == 1){
[self release]; self = nil;
return [[ClassC alloc] init];
}
return [self init];
}

Can I initiate an ivar indirectly?

I'm trying to initiate my ivar like this:
Declared like this in h-file
#interface MyClass: {
UITextView *_myTextView;
}
then created like this in the m-file
- (id)init {
self = [super init];
if(self) {
[self initTextView:_myTextView];
}
return self;
}
- (void)initTextView:(UITextView *)textView {
textView = [[UITextView alloc] init];
...
}
_myTextView will still be nil afterwards. Why is that and what should I do it to make it work? I've got ARC enabled.
[EDIT]
This works. Thanks all!
- (id)init {
self = [super init];
if (self) {
_textView1 = [self createTextView];
_textView2 = [self createTextView];
_textView3 = [self createTextView];
}
return self;
}
- (UITextView *)createTextView {
UITextView *textView = [[UITextView alloc] init];
...
return textView;
}
You need to always refer to instance variables using:
self.textView = [[UITextView alloc] init];
Also use a name other than initTextView as methods starting with init have special meaning in Objective-C.
If you want to use the same code to initialize multiple text view controls, then use code like this:
- (UITextView *)createTextView
{
UITextView *textView = [[UITextView alloc] init];
textView.something = whatever;
...
return textView;
}
And then use it like this:
- (id)init {
self = [super init];
if(self)
{
self.textView1 = [self createTextView];
self.textView2 = [self createTextView];
...
self.textViewN = [self createTextView];
}
}
In [self initTextView:_myTextView]; you pass the current value of _myTextView (which is nil) to your initTextView: method. To set the instance variable, you need a pointer to a pointer.
- (id)init {
self = [super init];
if (self) {
[self setupTextView:&_myTextView];
}
return self;
}
- (void)setupTextView:(UITextView * __strong *)textView {
*textView = [[UITextView alloc] init];
...
}
I also renamed the initTextView: method to setupTextView, as methods starting with init are expected to behave like other init methods in ARC.
- (id)init {
self = [super init];
if(self) {
[self initTextView];
}
}
- (void)initTextView{
_myTextView = [[UITextView alloc] init];
...
}
if you want to call initTextView for several text views , you can code like this :
- (id)init {
self = [super init];
if(self) {
_myTextView = [[UITextView alloc] init];
[self initTextView:_myTextView];
}
}
- (void)initTextView:(UITextView *)textView{
//setup the textView
...
}

How do I add another object to my singleton code?

Here is my singleton code:
#synthesize listOfSites;
+ (id)sharedInstance
{
static dispatch_once_t dispatchOncePredicate = 0;
__strong static id _sharedObject = nil;
dispatch_once(&dispatchOncePredicate, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
-(id)init
{
self = [super init];
if (self) {
listOfSites = [[NSMutableArray alloc] init];
}
return self;
}
#end
It's pretty much textbook... however, I want to add another array, similiar to "listOfSites" (call it "listOfReadings"). The code that says "if (self)" confuses me.
How do I add another array to this code?
if (self) { does nothing but to verify that the [super init] has worked - and that it hasn't return NULL or anything...
Other than that, you can do this normally, like :
declare a listOfReadings array (as an ivar/property?)
set it up
listOfReadings = [[NSMutableArray alloc] init];
or
listOfReadings = [[NSMutableArray alloc] initWithObjects:nil];
or (if it's a property)
[self setListOfReadings:[[NSMutableArray alloc] initWithObjects:nil]];
Example :
-(id) init {
self = [super init];
if (self) {
listOfSites = [[NSMutableArray alloc] init];
listOfReadings = [[NSMutableArray alloc] init];
}
return self;
}
after having declared the new NSMutableArray in your .h file :
NSMutableArray* listOfReadings;
-(id) init {
self = [super init];
if (self) {
listOfSites = [[NSMutableArray alloc] init];
listOfReadings = [[NSMutableArray alloc] init];
}
return self;
}
if (self) in other words means "If current object is successfully created then..."

Finding the cause of EXC_BAD_ACCESS

I have a class with the following init method:
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
StateStack* s = [[StateStack alloc] init];
state = s;
[s push:NONE]; //<--EXC_BAD_ACCESS on load here
[s release];
}
return self;
}
And StateStack has the following init code:
- (id)init {
self = [super init];
if (self) {
NSMutableArray* s = [[NSMutableArray alloc] init];
stack = s;
[s release];
NSLog(#"%d",[stack retainCount]);
}
return self;
}
Oddly, if I remove the NSLog line, the EXC_BAD_ACCESS moves to StateStack's dealloc method:
- (void)dealloc {
[stack release]; //<--EXC_BAD_ACCESS
[super dealloc];
}
Searching around seems to suggest that EXC_BAD_ACCESS is caused by overreleasing, but I can't see how I've overreleased anything. Does anyone know what the cause might be?
In your init function:
StateStack* s = [[StateStack alloc] init];
state = s;
[s push:NONE]; //<--EXC_BAD_ACCESS on load here
[s release];
you are allocating an instance of StateStack; this gets a retain count of 1. Then at the end of the function you call release, retain count goes to 0 and the object is ready to be released. So, when later dealloc is executed, the state ivar is sent another release and that is causing the bad access. You don't need to release s, since you want that state be retained. The same error pattern occurs in the other init method.
This would be correct:
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
StateStack* s = [[StateStack alloc] init];
state = s;
[s push:NONE]; //<--EXC_BAD_ACCESS on load here
}
return self;
}
- (id)init {
self = [super init];
if (self) {
NSMutableArray* s = [[NSMutableArray alloc] init];
stack = s;
}
return self;
}
NB: I don't want to generate misunderstandings. Using retain count to check for correct memory allocation is useless. This is true. Anyway, reasoning in terms of retain count helps understanding what happens when you allocate/release/autorelease an object. It is the basic mechanism, but it is too difficult to track it usage to check for correctness of memory management.
state = s is not copying the NSMutableArray object, it's just copying the pointer to it. So when you call [s release] the object referred to by both s and state is deallocated. You'll get an EXC_BAD_ACCESS whenever you use either from that point on.
Also, don't use [object retainCount] to debug memory management problems. It lies. Google NSZombies.
- (id)init{
self = [super init];
if (self) {
// Initialization code here.
state = [[StateStack alloc] init];
[state push:NONE];
}
return self;
}
StateStack
- (id)init {
self = [super init];
if (self) {
stack = [[NSMutableArray alloc] init];
}
return self;
}

TTLauncherItem: change badge immediately (or: how to refresh TTLauncherView)

I have a TTLauncherView with some TTLauncherItems. These show badges, representing messages from the network. I set the badges in viewWillAppear:, so if I switch to another view and then return, the correct badges are shown. But I want to update the badges as soon a message comes in.
Calling setNeedsDisplay on TTLauncherView doesn't help?
How can I refresh the TTLauncherView?
in my MessageReceiver class I do this:
TTNavigator* navigator = [TTNavigator navigator];
[(OverviewController *)[navigator viewControllerForURL:#"tt://launcher"] reloadLauncherView] ;
My TTViewController-derived OverviewController
#implementation OverviewController
- (id)init {
if (self = [super init]) {
self.title = OverviewTitle;
}
return self;
}
- (void)dealloc {
[items release];
[overView release];
[super dealloc];
}
-(void)viewDidLoad
{
[super viewDidLoad];
overView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
overView.backgroundColor = [UIColor whiteColor];
overView.delegate = self;
overView.columnCount = 4;
items = [[NSMutableArray alloc] init];
for(int i = 1; i <= NumberOfBars; ++i){
NSString *barID = [NSString stringWithFormat:NameFormat, IDPrefix, i];
TTLauncherItem *item = [[[TTLauncherItem alloc] initWithTitle:barID
image:LogoPath
URL:[NSString stringWithFormat:#"tt://item/%d", i]
canDelete:NO] autorelease];
[barItems addObject: item];
}
overView.pages = [NSArray arrayWithObject:items];
[self.view addSubview:overView];
}
-(void)viewWillAppear:(BOOL)animated
{
for(int i = 0; i <[barItems count]; i++){
TTLauncherItem *item = [items objectAtIndex:i];
NSString *barID = [NSString stringWithFormat:NameFormat, IDPrefix, i+1];
P1LOrderDispatcher *dispatcher = [OrderDispatcher sharedInstance];
P1LBarInbox *barInbox = [dispatcher.barInboxMap objectForKey:barID];
item.badgeNumber = [[barInbox ordersWithState:OrderState_New]count];
}
[super viewWillAppear:animated];
}
- (void)launcherView:(TTLauncherView*)launcher didSelectItem:(TTLauncherItem*)item
{
TTDPRINT(#"%#", item);
TTNavigator *navigator = [TTNavigator navigator];
[navigator openURLAction:[TTURLAction actionWithURLPath:item.URL]];
}
-(void)reloadLauncherView
{
[overView setNeedsDisplay];//This doesn't work
}
#end
I register my Controller with the LauncherView at the AppDelegate. In my messaging class I call [appDelegate reloadLauncherView]; that again will call this
-(void)reloadLauncherView
{
[self viewWillAppear:NO ];
}
on the Controller that contains the LauncherView.
I was having a very similar problem today, (modifying a TTLauncherItem, and not seeing my changes directly) and was able to solve it by making a call to [myLauncherView layoutSubviews]; BEFORE I modified the TTLauncherItem. I actually tracked it down in the code, and this was because layoutSubviews will re-create the LauncherView's _buttons array (which is what needed to happen, in my case).