I have a pretty big method here that I think has a leak somewhere.
Running the allocation instrument shows that every time a new "category" is passed to this method, the amount of memory being taken up increases. This shouldn't happen, as concurrently with a "category" being opened, another one is getting closed, and thus the amount of memory being taken up should remain fairly steady.
Something interesting about it is that the memory being taken up only increases if the category has not yet been opened before. Does anyone have any suggestions as to why this would be?
Header with my IVars.
The method:
Any help would be much appreciated!
Running the allocation instrument shows that every time a new "category" is passed to this method, the amount of memory being taken up increases. This shouldn't happen, as concurrently with a "category" being opened, another one is getting closed, and thus the amount of memory being taken up should remain fairly steady.
Something interesting about it is that the memory being taken up only increases if the category has not yet been opened before. Does anyone have any suggestions as to why this would be?
Header with my IVars.
Code:
@interface Keyboard : UIScrollView
{
NSArray *characters;
NSMutableArray *favorites;
NSTimer *holdTimer;
BOOL justFavored;
BOOL timerToStop;
BOOL viewingFavorites;
UIButton *spaceKey;
UIButton *deleteKey;
}
The method:
Code:
- (void)openCategory:(NSString*)category
{
[self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
{
if ([obj isKindOfClass:[UIButton class]])
{
[obj removeFromSuperview];
}
}];
if ([category isEqualToString:@"Favorites"])
{
characters = favorites;
self.backgroundColor = [UIColor colorWithRed:0.72 green:0.53 blue:0.15 alpha:1.0];
viewingFavorites = YES;
}
else
{
NSString *path = [[NSBundle mainBundle] pathForResource:category ofType:@"txt"];
NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF16BigEndianStringEncoding error:NULL];
characters = [string componentsSeparatedByString:@"\n"];
self.backgroundColor = [UIColor colorWithRed:0.22 green:0.33 blue:0.83 alpha:1.0];
viewingFavorites = NO;
}
[characters enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
{
UIButton *key = [UIButton buttonWithType:UIButtonTypeRoundedRect];
key.frame = CGRectMake((index%6)*(self.frame.size.width/6.0),
(floor(index/6.0))*(self.frame.size.width/6.0),
self.frame.size.width/6.0,
self.frame.size.width/6.0);
[key setTitle:obj forState:UIControlStateNormal];
[key setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
if ([favorites containsObject:obj])
{
[key setBackgroundImage:[UIImage imageNamed:@"favorNormal"] forState:UIControlStateNormal];
[key setBackgroundImage:[UIImage imageNamed:@"favorPress"] forState:UIControlStateHighlighted];
}
else
{
[key setBackgroundImage:[UIImage imageNamed:@"keyNormal"] forState:UIControlStateNormal];
[key setBackgroundImage:[UIImage imageNamed:@"keyPress"] forState:UIControlStateHighlighted];
}
[key addTarget:self action:@selector(startTimer:) forControlEvents:UIControlEventTouchDown];
[key addTarget:self action:@selector(stopTimer) forControlEvents:UIControlEventTouchDragExit];
[key addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:key];
}];
spaceKey = [UIButton buttonWithType:UIButtonTypeRoundedRect];
spaceKey.frame = CGRectMake(self.frame.size.width/6.0,
(ceil([characters count]/6.0))*(self.frame.size.width/6.0),
self.frame.size.width*2.0/3.0,
self.frame.size.width/6.0);
[spaceKey setTitle:@" " forState:UIControlStateNormal];
[spaceKey addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:spaceKey];
deleteKey = [UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteKey.frame = CGRectMake(self.frame.size.width*5.0/6.0,
(ceil([characters count]/6.0))*(self.frame.size.width/6.0),
self.frame.size.width/6.0,
self.frame.size.width/6.0);
[deleteKey setTitle:@"⌫" forState:UIControlStateNormal];
[deleteKey setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[deleteKey setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
deleteKey.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
[deleteKey addTarget:self action:@selector(deletePress) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:deleteKey];
if (viewingFavorites == YES)
{
[spaceKey setBackgroundImage:[[UIImage imageNamed:@"favorNormal"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateNormal];
[spaceKey setBackgroundImage:[[UIImage imageNamed:@"favorPress"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateHighlighted];
[deleteKey setBackgroundImage:[UIImage imageNamed:@"favorNormal"] forState:UIControlStateNormal];
[deleteKey setBackgroundImage:[UIImage imageNamed:@"favorPress"] forState:UIControlStateHighlighted];
}
else
{
[spaceKey setBackgroundImage:[[UIImage imageNamed:@"keyNormal"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateNormal];
[spaceKey setBackgroundImage:[[UIImage imageNamed:@"keyPress"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateHighlighted];
[deleteKey setBackgroundImage:[UIImage imageNamed:@"keyNormal"] forState:UIControlStateNormal];
[deleteKey setBackgroundImage:[UIImage imageNamed:@"keyPress"] forState:UIControlStateHighlighted];
}
self.contentSize = CGSizeMake(self.frame.size.width, ((ceil([characters count]/6.0))+1)*(self.frame.size.width/6.0));
[self flashScrollIndicators];
}
Any help would be much appreciated!