We can't help you unless we see your code. Don't forget that all autorelease does is automatically decrement your reference counts. It won't cause memory to be freed if it has been retained somewhere else.
#import "ToolBar.h"
#import "ColorController.h"
#import "FadeController.h"
@implementation ToolBar
- (void)awakeFromNib
{
ColorItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"Color"];
[ColorItem setToolTip:@"Select Light Color"];
[ColorItem setPaletteLabel:[ColorItem label]];
[ColorItem setImage:[NSImage imageNamed:@"ColorLightToolBar.png"]];
[ColorItem setTarget:self];
[ColorItem setAction:@selector(selectColorTool:)];
WhiteItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"White"];
[WhiteItem setToolTip:@"Reset To White Light"];
[WhiteItem setPaletteLabel:[WhiteItem label]];
[WhiteItem setImage:[NSImage imageNamed:@"WhiteLightToolBar.png"]];
[WhiteItem setTarget:self];
[WhiteItem setAction:@selector(selectWhiteTool:)];
FullScreenItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"Full Screen"];
[FullScreenItem setToolTip:@"Full Screen Mode"];
[FullScreenItem setPaletteLabel:[FullScreenItem label]];
[FullScreenItem setImage:[NSImage imageNamed:@"FullScreenToolBar.png"]];
[FullScreenItem setTarget:self];
[FullScreenItem setAction:@selector(fullScreenModeTool:)];
[self setupToolbar];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
if ([itemIdentifier isEqualToString:@"Color"])
{return ColorItem;}
if ([itemIdentifier isEqualToString:@"White"])
{return WhiteItem;}
if ([itemIdentifier isEqualToString:@"Full Screen"])
{return FullScreenItem;}
return [COLOR="blue"][item autorelease][/COLOR];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray arrayWithObjects:NSToolbarSeparatorItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier, @"Color", @"White", @"Full Screen", nil];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray arrayWithObjects: NSToolbarFlexibleSpaceItemIdentifier, @"Color", @"White", @"Full Screen", NSToolbarFlexibleSpaceItemIdentifier, nil];
}
- (void)setupToolbar
{
toolbar = [[NSToolbar alloc] initWithIdentifier:@"Toolbar"];
[toolbar setDelegate:self];
[toolbar setAutosavesConfiguration:YES];
[mainWindow2 setToolbar:[COLOR="Blue"][toolbar autorelease][/COLOR]];
}
- (IBAction)selectColorTool:(id)sender
{
[myColorTool selectColor:nil];
}
- (IBAction)selectWhiteTool:(id)sender
{
[myWhiteTool selectWhite:nil];
}
- (IBAction)fullScreenModeTool:(id)sender
{
[myFullscreenTool fullScreenMode:nil];
}
# pragma mark Dealloc
[COLOR="blue"]- (void)dealloc
{
[toolbar autorelease];
[NSToolbarItem autorelease];
[super dealloc];
}[/COLOR]
@end
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
if ([itemIdentifier isEqualToString:@"Color"])
{return ColorItem; //if we get here, we exit this function without ever autoreleasing item }
if ([itemIdentifier isEqualToString:@"White"])
{return WhiteItem; //if we get here, we exit this function without ever autoreleasing item }
if ([itemIdentifier isEqualToString:@"Full Screen"])
{return FullScreenItem; //if we get here, we exit this function without ever autoreleasing item }
return [item autorelease];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
if ([itemIdentifier isEqualToString:@"Color"])
{return [ColorItem autorelease];}
if ([itemIdentifier isEqualToString:@"White"])
{return [WhiteItem autorelease];}
if ([itemIdentifier isEqualToString:@"Full Screen"])
{return [FullScreenItem autorelease];}
return [item autorelease];
}
- (void)dealloc
{
[ColorItem autorelease];
[WhiteItem autorelease];
[FullScreenItem autorelease];
[toolbar release];
[NSToolbarItem release];
[super dealloc];
}
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemI
return item;
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
if ([itemIdentifier isEqualToString:@"Color"])
{return ColorItem;}
if ([itemIdentifier isEqualToString:@"White"])
{return WhiteItem;}
if ([itemIdentifier isEqualToString:@"Full Screen"])
{return FullScreenItem;}
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
return item;
}
- (void)dealloc
{
[super dealloc];
}
but i still have a memory leak that i don't understand... what on earth are "general blocks"? this leak always seems to stay at 128 bytes, so it sounds pretty negligible to me - but maybe it will cause problems later down the road? besides, since i'm learning still i'd like to understand it... but maybe this super small memory leak is normal?
Nope, sorry, no leak is normal. It may be innocuous if they're small amounts of memory over a short-lived application, but you want to eradicate all of them. I'm taking a SWAG (scientific wild assed guess) that GeneralBlock means any memory allocated using C's alloc functions not being free()'d (malloc, calloc, realloc, etc). You doing that anywhere in your code?
Cocoa itself has minor leaks that show up in some situations, btw.
so these "general block" memory leaks at 128 bytes are normal?
there are lots of objects created by alloc in my code, but i'm pretty sure i've released them all... it would be more helpful if Instruments could be more specific (??)...
I think you can click the Extended Detail button on the bottom toolbar of the screen to see a stack trace of what line of code the leak was detected on. There's a good blog post on how to do just this here.
HTH,
[
Enuratique
- (void)awakeFromNib
{
if ([[prefsRadioGroup cellWithTag:1] state] == NSOnState)
{
SetSystemUIMode(kUIModeNormal, 0);
[mainWindow2 setAlphaValue:0.0];
[mainWindow2 makeKeyAndOrderFront:nil];
[COLOR="Blue"][mainWindow2 setContentView:normalscreenView];[/COLOR]
timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(normalScreenfadeIN2:) userInfo:nil repeats:YES] retain];
}
}
When you call setContentView, if there was an existing content view already set then it will be released. If that released view has a member variable that it doesn't properly clean up in it's dealloc, that could be the cause.
So in every function you should place the returned value in an autorelease pool, and in every dealloc: method, you should release all of the properties to get rid of memory leaks? If that can cause it... I know where my leaks are coming from!
-.h file-
NSData *someStuff;
@property (nonatomic, retain) NSData *someStuff;
-.m file-
@synthesize someStuff;
- (id)init {
self.someStuff = [NSData data];
}
- (void)dealloc {
[someStuff release]; /* NOT CALLING THIS WILL CAUSE A LEAK BECAUSE THE POINTER WAS RETAINED BY THE PROPERTY EVEN THOUGH WHAT WE GOT OUT OF THE NSDATA STATIC INITIALIZER WAS AUTORELEASED */
[super dealloc];
}
When you call setContentView, if there was an existing content view already set then it will be released. If that released view has a member variable that it doesn't properly clean up in it's dealloc, that could be the cause.
- (IBAction)closePrefsWindow:(id)sender
{
timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(preferenceWindowFadeOUT:) userInfo:nil repeats:YES] retain];
}
- (void)preferenceWindowFadeOUT:(NSTimer *)theTimer
{
NSPanel* preferenceWindow;
if ([preferenceWindow alphaValue] > 0.0)
{
[preferenceWindow setAlphaValue:[preferenceWindow alphaValue] -0.1];
}
else
{
[timer invalidate];
[timer release];
[preferenceWindow close];
timer = nil;
}
}
- (void)dealloc
{
[timer release];
[super dealloc];
}
now, just out of curiousity i have removed all my dealloc methods from all of my classes and i still receive the same memory leaks, instead of receiving more as i had assumed since i was under the impression that if something is released it has to be also released thru the dealloc method of the same class, otherwise i won't be released... this is clearly not the case...
- (void) setTheory: (Theory *)newTheory
{
Theory *oldTheory = nil;
if ( theory != newTheory ) // If they're the same, do nothing
{
[self willChange]; // For Enterprise Objects only
oldTheory = theory; // Copy the reference
theory = [newTheory retain];// First retain the new object
[oldTheory release]; // Then release the old object
}
return;
}
my content view is never released. it's always the same throughout the application.
Could you have set one up in Interface Builder, in which case there would be one set when your awakeFromNib is called?
The dealloc is called when an object's retain count is 0. If your code happens to release objects before the containing class is released, then it's not necessary to release them in your dealloc method. It's just good practice to ensure that all objects have release called on them in dealloc.