discuss-gnustep
[Top][All Lists]
Advanced

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

Need advice about NSTextField, performClick IBAction, and memory managem


From: Bertrand Dekoninck
Subject: Need advice about NSTextField, performClick IBAction, and memory management
Date: Thu, 13 Dec 2018 21:55:19 +0100

Hi everyone,
I'm reading the "Cocoa programming" book from Aaron Hillegass and I am facing two problems writing a little tasklist program at chapter 6.

1) First problem. The interface has a NSTextField to write a task, an "add" NSButton and a NSTableView that lists the tasks.

The "add" button uses a "createNewTask" method written in my appController, that adds the textValue from the textField to my datasource of the tableView and reloads the view..

In InterfaceBuilder (not Gorm here but it should be the same), I've also connected the "performClick" action from the textField to the addButton, so that the createTask method is also called when I type on the "Enter" key of the keyboard after editing the textField.

It works as expected, but I've got extra calls to createNewTask : when the textField has the focus, performClick is also sent when clicking on another widget or if I use the "tab" key to cycle into another widget.

Is there a way to avoid these ? Or should I disconnect the performClick action and create another way to call createNewTask with the "enter" key in the NSTextField?

2) The second problem is a memory management problem : in the createNewTask, I create a NSString using alloc +init.

If I release that string at the end of the method, I've got a segfault if I try to add more than two tasks in the tasklist

Obviously, I was trying to access a string that had been deallocated. Why does this occur with the third call (not the second call) to the method ?

If I don't release the string, everything works fine.

Is there a memory leak if I don't release ? And if true, when should I release the string ?


Bertrand Dekoninck

PS : here attached my appController.m file

Attachment: AppController.m
Description: Binary data



reply via email to

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