discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need advice about NSTextField, performClick IBAction, and memory man


From: David Chisnall
Subject: Re: Need advice about NSTextField, performClick IBAction, and memory management
Date: Fri, 14 Dec 2018 08:23:28 +0000
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.3.3

On 14/12/2018 06:09, Josh Freeman wrote:
    These three lines in createNewTask: cause the issue:

         NSString *string = [[NSString alloc] init];
         string = [textField stringValue];
     ...
    [string release];// With this, I've got an "exec_BAD_ACCESS" error when calling three times in a row createNewTask with the same string in textField


   The first line sets 'string' to a retained, empty string object. The next line sets 'string' to the object returned by [textField stringValue], leaking the previous string object (its address was forgotten while it was still retained). 'String' then points to an object that wasn't retained by your code, so sending it a release message will cause it to deallocate while it's still referenced elsewhere (segfaulting if it's accessed after that).

   You can fix the issue by removing the first & third lines (empty-string allocation, release call), and moving the 'string' var definition to the second line:

Note: you can also fix the issue by removing all references to retain / release and compiling with -fobjc-arc. There is absolutely no reason for not doing this in new Objective-C code. It will work with macOS, iOS, watchOS, tvOS, and GNUstep, and the compiler will generate code that is faster, smaller, and more likely to be correct than if you do this by hand.

David



reply via email to

[Prev in Thread] Current Thread [Next in Thread]