1

I have the following function:

func delete()
{
    if (deleteCounter == 0)
    {
        print("Deletion Start")
        deleteCounter = 1
        // Execute async delete task
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
            {
                if (self.textDocumentProxy.isProxy())
                {
                    for _ in 1..<50
                    {
                        (self.textDocumentProxy as UIKeyInput).deleteBackward()
                    }
                }
                print("Deletion End")
                self.deleteCounter = 0
        })
    } else
    {
        print("Denying deletion")
    }
}

where deleteCounter is a integer that is set to zero by default.

The code is supposed to function so when the delete key on my custom keyboard app extension is pressed, it will not call the delete loop if it is still looping, thus preventing too many method calls.

The code seems to be working fine, but if I hit the delete button rapidly (thus calling this function repeatedly) for a large period of time I get the following error:

Thread 3: EXC_BAD_ACCESS(code = 1,address=0x10)

on this line:

(self.textDocumentProxy as UIKeyInput).deleteBackward()

I do not ever modify the self.textDocumentProxy object because it is just a default object that IOS provides for keyboard extensions that you can always access.

After reading up on the EXC_BAD_ACCESS error I figured out that it was probably because the self.textDocumentProxy object was being released from memory at some point.

How do I prevent self.textDocumentProxy from being released, or how do I make sure that I can always get a non-released copy of the object?

UPDATE:

I enabled zombie objects and I get the following error message:

[_UIInputViewControllerOutput setKeyboardOutputs:]: message sent to deallocated instance 0x146435b10

This seems to confirm my suspicion that self.textDocumentProxy is being de-allocated.

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • Why not use a bool rather than an Int, but more importantly why are you dispatching the delete onto another queue? You are asking for concurrency problems and it doesn't seem like deleting a character would be an expensive or long running task – Paulw11 Sep 02 '16 at 02:53
  • I am using an int right now because I might eventually change my deletion system so that a bool will not be sufficient. Additionally, I am deleting the character in a separate thread because that is the only way I could figure out how to implement a feature where the delete button would not be pressed if it was still deleting characters. – Foobar Sep 02 '16 at 13:01

0 Answers0