[Top][All Lists]

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

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

From: bertrand . dekoninck
Subject: Re: Need advice about NSTextField, performClick IBAction, and memory management
Date: Fri, 14 Dec 2018 13:23:39 +0100

Envoyé de mon iPhone

> Le 14 déc. 2018 à 09:23, David Chisnall <address@hidden> a écrit :
>> 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
> __
Hillegass' book is several years old now. I've begun another one on Objc2 but 
I'm not at the arc section yet. Also, I want my code to run on ppc where objc2 
isn't an option yet.

> _____________________________________________
> Discuss-gnustep mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/discuss-gnustep

reply via email to

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