[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Need advice about NSTextField, performClick IBAction, and memory man
From: |
Riccardo Mottola |
Subject: |
Re: Need advice about NSTextField, performClick IBAction, and memory management |
Date: |
Fri, 14 Dec 2018 10:20:33 +0100 |
User-agent: |
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.4 |
Hi Bertrand,
Josh Freeman wrote:
You can set the textfield to only send its action when Enter's
pressed (instead of every time editing finishes): In Interface
Builder, select the textfield, then show the Inspector panel. Under
the Inspector's Attributes tab, switch the textfield's 'Send Action'
mode from "End editing" to "Enter only". (You can also do this
programmatically by sending the textfield's cell a
setSendsActionOnEndEditing:NO message.)
You have the same option in Gorm. Select the TextField, go to the
inspector and in the attributes you have "Send action on" Enter only
(what you probably want) or "End Editing" which is essentially always
when your control looses focus.
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
You are trying to release the textField value, not the empty one you
allocated. By assigning the the value you overwrite your own object.
Actually, this code will leak memory: the first allocated string.
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:
NSString *string = [textField stringValue];
Another way to explain what Josh is saying:
You need to alloc+init a string if you want to work on it, but in this
case, you declatr *string just as a pointer to get the value of the
textField, so you don't own the value and have no need to free it.
Conversely, if you need to "work" on the value retturned by the
textField for more than a run-loop, or pass it to a thread or to be
blunt "for a long time", then you need to either retain or copy it. E.g.
you get a value from a window, then close the window but want to
continue working on that. If you retain or copy your object, then you
need to release it.
Riccardo
- Need advice about NSTextField, performClick IBAction, and memory management, Bertrand Dekoninck, 2018/12/13
- Re: Need advice about NSTextField, performClick IBAction, and memory management, Josh Freeman, 2018/12/14
- Re: Need advice about NSTextField, performClick IBAction, and memory management, David Chisnall, 2018/12/14
- Re: Need advice about NSTextField, performClick IBAction, and memory management,
Riccardo Mottola <=
- Re: Need advice about NSTextField, performClick IBAction, and memory management, Bertrand Dekoninck, 2018/12/14