1) StackOverflow suggests using a local @autoreleasepool{} :http://stackoverflow.com/questions/23299562/how-to-deallocate-objects-in-nsmutablearray-with-arc

>(1) array is a class method for NSArray and creates an NSArray and NOT an NSMutableArray. Since NSMutableArray inherits from NSArray, the code will run but you won't have a mutable array. 
I disagree with this statement.  [NSMutableArray array] works fine here.  Note that it doesn't need a release in non-ARC applications.

I was unable to reproduce an Activity Monitor memory hog with this FB (non-ARC) code (works equally as well without the @autoreleasepool{} block):

include "NSLog.incl"

BeginCDeclaration
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
  NSWindow *window;
  NSMutableArray *mymutarr;
}
@end
EndC

BeginCFunction
@implementation AppDelegate

- (void) dealloc
{
 [window release];
 [super dealloc];
}

- (void) buildMenu
{
 NSMenu *menubar = [[NSMenu new] autorelease];
 [NSApp setMainMenu:menubar];
 NSMenuItem *menuBarItem = [[NSMenuItem new] autorelease];
 [menubar addItem:menuBarItem];
 NSMenu *appMenu = [[NSMenu new] autorelease];
 [menuBarItem setSubmenu:appMenu];
 [appMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
}

- (void) buildWindow
{
 #define _wndW  250
 #define _wndH  150

 window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
                       styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask
                       backing: NSBackingStoreBuffered
                       defer: NO];

 [window center];
 [window setTitle: @"Test window"];
 [window makeKeyAndOrderFront: nil];

// ***** Quit btn ***** //
 NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 60, 10, 40, 40 )];
 [quitBtn setBezelStyle:NSCircularBezelStyle ];
 [quitBtn setTitle: @"Q" ];
 [quitBtn setAction:@selector(terminate:)];
 [[window contentView] addSubview: quitBtn];
 [quitBtn release];
}

- (void) applicationWillFinishLaunching: (NSNotification*)notification
{
 [self buildMenu];
 [self buildWindow];
}

- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
@autoreleasepool {
 mymutarr = [NSMutableArray array];
 for (int i=0; i<150000; i++) {
  [mymutarr addObject:@"test: a very long string"];
 }
 [mymutarr removeAllObjects];
 mymutarr = nil;
}
NSLog(@"%@",mymutarr);
}
@end

EndC

BeginCCode
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[[AppDelegate alloc] init] autorelease];
[application setDelegate:appDelegate];
[application run];
[pool drain];
EndC



-----Original Message-----
From: Brian S <fblistserve@...>
To: futurebasic <futurebasic@...>
Sent: Tue, Mar 7, 2017 1:23 pm
Subject: Re: [FB] Objective C - ivar dealoc from memory


Victor wrote:

How can I release from memory an instance variable using Objective C ARC.

Typically, you don't; that's the point of ARC. However, it is possible to change your project to non-ARC by going into Build Settings and turning it off both project and target.


After instanciating the variable [NSMutableArray array].

Two things with this line:
(1) array is a class method for NSArray and creates an NSArray and NOT an NSMutableArray. Since NSMutableArray inherits from NSArray, the code will run but you won't have a mutable array. 

(2) You probably want [NSMutableArray arrayWithCapacity:0];  but there at least one other issue ( read on )

Keep in mind these are class methods, so they are owned by the autorelease pool and will be automatically eligible for release at the end of the current code scope ( in this case at the end of applicationDidFinishLaunching: )


At the end of “applicationDidFinishLaunching” function i do:
[mymutarr removeAllObjects];
mymutarr = nil;

Those two lines of code might make sense for a mutable array that was allocated/inited and you need to keep it alive. The class method ( a so-called convenience method ) will be automatically released, so removeAllObjects and NULLing the array is redundant. In some cases ( not in your example code ) it might be error to code those two lines since the timing of the array disposal isn't known exactly




But in “Activity Monitor” my application will use the maximum value memory allocated.
Post a screen print. Your message is not clear as written.


self->mymutarr = [NSMUtableArray array];

The '->' mostly wrong. This is a pointer to a struct and only in rare circumstances would an app need to dereference the pointer to an NSMutableArray or most other 'NS' objects.

Brian


--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@... To access the list archives, go to: http://freegroups.net/groups/futurebasic/


--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@... To access the list archives, go to: http://freegroups.net/groups/futurebasic/


=