im writing a video recorder application for mac with cocoa, and have a huge problem with memory usage.
When launching the app, i have a window with two buttons that i can change view with and a simple custom view where the webcam get loaded. Just this view takes 21mb of ram, when i press on one of the buttons i change the custom view to a QTMovie of the content recorded from the cam.
Every time i change view my app fills upp with about 10mb of ram, and never stops. I have garbage collection on. Even if I show the view of the QTMovie and wants to press the button to display it again, its takes more ram.
Code that loads the subview, trigged from the button
if ([myCurrentViewController view] != nil)
[[myCurrentViewController view] removeFromSuperview];
NSViewController* cameraViewController2 = [[NSViewController alloc] initWithNibName:@"kVideo" bundle:nil];
if (cameraViewController2 != nil)
{
myCurrentViewController = cameraViewController2;
}
[myTargetView addSubview: [myCurrentViewController view]];
And this is what happens when the the videoView loads, and this takes 10mb of ram every time i want it to display.
-(void)playMovie
{
[movieView setMovie:nil];
NSString* moviePath = [NSBundle pathForResource:@"tempFile" ofType:@"mov" inDirectory:@"/Users/Shared/"];
QTMovie* movie = [[QTMovie alloc] initWithFile:moviePath error:nil];
[movieView setMovie:movie];
}
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent3:) name:@"updateVideoPlayer" object:nil];
[self playMovie];
}
Okay i solved it, every time i pressed a button to change a view, i allocated the new view with something like this
SViewController* cameraViewController2 = [[NSViewController alloc] initWithNibName:@"kVideo" bundle:nil];, and they never got released or thrown away or whatever they should do. Now instead i allocate my views in one place, and just refer to the allocated views when i press new buttons.