Reputation: 1560
I have a small issue with memory with the current code I have written, I'm new to cocoa and programming with object orientated languages, so currently Xcode does all the memory management stuff for me, however that's not helping me right now.
The problem currently is that I am creating a NSTableView and it has pictures in each cell that update in 0.5s intervals, this is fine, however each time the image get's loaded a new NSImageView object gets created (this is were i think the memory issue is, correct me if I'm wrong), how ever I lack a solution to this problem. This is causing the memory usage to be increased by about 5MB per 0.5s and so after 10 minutes or so the application will be taking up a large portion of my computers memory. I don't know how to get rid of the created NSImageView objects after they are useless, without causing the images to be delayed during the display process...
This is the current code:
-(void)play_view:(NSInteger)view{
valid_times_counter = valid_times_counter + 1;
if(valid_times_counter == ([valid_times count]-2)){
valid_times_counter = 1;
}
[self performSelector:@selector(reload_table_data) withObject:self afterDelay:0.5];
}
-(void)reload_table_data{
[view_table removeFromSuperview];
[self addSubview:view_table];
[view_table reloadData];
[self play_view:valid_times_counter];
}
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"drawing"); //Table being drawn, probably not relevant but just in case ;)
[[NSColor colorWithCalibratedRed:0.01f green:0.40f blue:0.69f alpha:1.00f]setFill];
NSRect bg = NSMakeRect(0, 0, self.frame.size.width, self.frame.size.height);
NSRectFill(bg);
if(level == 1){
NSLog(@"draw_table Level 1");
[view_table removeFromSuperview];
[self setFrameSize:NSMakeSize(2048, 1024)];
view_table = [[NSTableView alloc]initWithFrame:NSMakeRect(0, self.frame.size.height-1024, 2048, 1024)];
for(int c = 0; c<4;c++){
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%i",c]];
[[column headerCell] setStringValue:[NSString stringWithFormat:@"%i",c]];
[column setWidth:512.0];
[view_table addTableColumn:column];
}
[view_table setRowHeight:512];
[view_table setDelegate:(id)self];
[view_table setDataSource:(id)self];
[view_table setIntercellSpacing:NSMakeSize(0, 0)];
[view_table setIdentifier:@"view_table"];
[view_table reloadData];
[self addSubview:view_table];
}
else if(level == 2){
NSLog(@"draw_table Level 2");
[view_table removeFromSuperview];
[self setFrameSize:NSMakeSize(4096, 2048)];
view_table = [[NSTableView alloc]initWithFrame:NSMakeRect(0, self.frame.size.height-2048, 4096, 2048)];
for(int c = 0; c<8;c++){
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%i",c]];
[[column headerCell] setStringValue:[NSString stringWithFormat:@"%i",c]];
[column setWidth:512.0];
[view_table addTableColumn:column];
}
[view_table setRowHeight:512];
[view_table setDelegate:(id)self];
[view_table setDataSource:(id)self];
[view_table setIntercellSpacing:NSMakeSize(0, 0)];
[view_table setIdentifier:@"view_table"];
[view_table reloadData];
[self addSubview:view_table];
}
else if(level == 3){
NSLog(@"draw_table Level 3");
[view_table removeFromSuperview];
[self setFrameSize:NSMakeSize(8192, 4096)];
view_table = [[NSTableView alloc]initWithFrame:NSMakeRect(0, self.frame.size.height-4096, 8192, 4096)];
for(int c = 0; c<16;c++){
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%i",c]];
[[column headerCell] setStringValue:[NSString stringWithFormat:@"%i",c]];
[column setWidth:512.0];
[view_table addTableColumn:column];
}
[view_table setRowHeight:512];
[view_table setBackgroundColor:[NSColor clearColor]];
[view_table setDelegate:(id)self];
[view_table setDataSource:(id)self];
[view_table setIntercellSpacing:NSMakeSize(0, 0)];
[view_table setIdentifier:@"view_table"];
[view_table reloadData];
[self addSubview:view_table];
}
else if(level == 4){
NSLog(@"draw_table Level 4");
[view_table removeFromSuperview];
[self setFrameSize:NSMakeSize(16384, 8192)];
view_table = [[NSTableView alloc]initWithFrame:NSMakeRect(0, self.frame.size.height-8192, 16384, 8192)];
for(int c = 0; c<32;c++){
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%i",c]];
[[column headerCell] setStringValue:[NSString stringWithFormat:@"%i",c]];
[column setWidth:512.0];
[view_table addTableColumn:column];
}
[view_table setRowHeight:512];
[view_table setDelegate:(id)self];
[view_table setDataSource:(id)self];
[view_table setIntercellSpacing:NSMakeSize(0, 0)];
[view_table setIdentifier:@"view_table"];
[view_table reloadData];
[self addSubview:view_table];
}
else if(level == 5){
NSLog(@"draw_table Level 5");
[view_table removeFromSuperview];
[self setFrameSize:NSMakeSize(32768, 16384)];
view_table = [[NSTableView alloc]initWithFrame:NSMakeRect(0, self.frame.size.height-16384, 32768, 16384)];
for(int c = 0; c<64;c++){
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%i",c]];
[[column headerCell] setStringValue:[NSString stringWithFormat:@"%i",c]];
[column setWidth:512.0];
[view_table addTableColumn:column];
}
[view_table setRowHeight:512];
[view_table setBackgroundColor:[NSColor clearColor]];
[view_table setDelegate:(id)self];
[view_table setDataSource:(id)self];
[view_table setIntercellSpacing:NSMakeSize(0, 0)];
[view_table setIdentifier:@"view_table"];
[view_table reloadData];
[self addSubview:view_table];
}
//Problem section
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
if([tableView.identifier isEqualToString: @"view_table"]){
NSLog(@"---------VIEW---------");
NSView *view = [tableView makeViewWithIdentifier:@"MyView2" owner:self];
[tableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
if (view == nil) {
view = [[NSView alloc]initWithFrame:NSMakeRect(0, 0, 512, 512)];
NSString *table_id;
if([tableColumn.identifier integerValue]<10){
table_id = [NSString stringWithFormat:@"000%@",tableColumn.identifier];
}
else if([tableColumn.identifier integerValue]<100){
table_id = [NSString stringWithFormat:@"00%@",tableColumn.identifier];
}
else if([tableColumn.identifier integerValue]<1000){
table_id = [NSString stringWithFormat:@"0%@",tableColumn.identifier];
}
NSString *row_id;
if(row < 10){
row_id = [NSString stringWithFormat:@"000%li",row];
}
else if(row < 100){
row_id = [NSString stringWithFormat:@"00%li",row];
}
else if(row < 1000){
row_id = [NSString stringWithFormat:@"0%li",row];
}
if([valid_times count]>1){
image_view2 = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 0, 512, 512)]; //This gets repeated every 0.5s, and this is what is causing the problem I think...
NSString *url_str = [self get_url:(valid_times_counter):table_id:row_id];
NSLog(@"Loading Image from: %@", url_str);
NSImage *map_tile2 = [[NSImage alloc] initWithContentsOfFile:url_str];
[image_view2 setImage:map_tile2];
[view addSubview:image_view2];
}
else{
//No Views found
NSLog(@"ERROR, NO IMAGES FOUND 348.map_view.h-tableView.viewForTableColumn:row For data %@", [valid_times objectAtIndex:0]);
}
}
return view;
I have commented the section in the code where I think the problem is, does any one know a solution, I thought of just changing the contents of the cell instead of reloading the table data every time, but I don't really know how to do that.
If you require more detail just ask, I'd be great full about any ideas...
Upvotes: 0
Views: 406
Reputation: 29524
On each line, you seem to be re-allocating your table view.
view_table = [[NSTableView alloc]initWithFrame:NSMakeRect(0, self.frame.size.height-4096, 8192, 4096)];
Leaving aside the extremely odd numbers for the frame size, you should not be recreating your table view. If you want to reload the data, just call -reloadData
, but do not keep removing and adding the table view. I don't know if you're using ARC or not, but most likely your problem is that you're not releasing the old memory that the table view took up, but instead you're assigning a new instance to the old ivar. The old instance is still hanging around somewhere, but you cannot access it because you've reassigned the pointer.
You're also doing all this strange reloading stuff in -drawRect:
which is horrible for so many reasons.
Upvotes: 1
Reputation: 989
Do you have to call alloc/init on image_view2 at all? Since you are hard coding the size every time, it seems you can just set the new image?
Upvotes: 1