Note that there are some explanatory texts on larger screens.

plurals
  1. POCocoa - Memory Issues - NSTableView
    text
    copied!<p>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.</p> <p>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...</p> <p>This is the current code:</p> <pre><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&lt;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&lt;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&lt;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&lt;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&lt;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]&lt;10){ table_id = [NSString stringWithFormat:@"000%@",tableColumn.identifier]; } else if([tableColumn.identifier integerValue]&lt;100){ table_id = [NSString stringWithFormat:@"00%@",tableColumn.identifier]; } else if([tableColumn.identifier integerValue]&lt;1000){ table_id = [NSString stringWithFormat:@"0%@",tableColumn.identifier]; } NSString *row_id; if(row &lt; 10){ row_id = [NSString stringWithFormat:@"000%li",row]; } else if(row &lt; 100){ row_id = [NSString stringWithFormat:@"00%li",row]; } else if(row &lt; 1000){ row_id = [NSString stringWithFormat:@"0%li",row]; } if([valid_times count]&gt;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; </code></pre> <p>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.</p> <p>If you require more detail just ask, I'd be great full about any ideas...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload