0

I have a class, clickableImage. clickableImage has a callback variable for a function.

When you assign the callback function, I add a gesture recoginizer.

clickableImage has a function 'tapped' which just listens for the tap event as illustrated below.

 private func tapped(tap:UITapGestureRecognizer)
    {
        println("Here")
        if(_touchCallback != nil)
        {
            touchCallback(self)
        }
    }

    var touchCallback:((K_PreviewImage)->Void)
    {
        set{
            if(_touchCallback == nil)
            {
                var tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action:"tapped:")
                self._image.addGestureRecognizer(tap)
            }

            _touchCallback = newValue
        }
        get{
            return _touchCallback
        }
    }

When I tap this image, the app crashes, with only (llb). The println() does not get called. I tried enabling zombies and I 'SOMETIMES' get a message "message sent to deallocated instance".

The image is NOT delloacted, otherwise I wouldn't be able to CLICK on it!

If you have ANY idea what is going on, you would be a live saver

Aggressor
  • 13,323
  • 24
  • 103
  • 182

1 Answers1

1

I will give you couple examples that will cause "message sent to deallocated instance".

 class ViewController : UIViewController {

    override func viewDidLoad() {
       super.viewDidLoad()
       // Some other class that has gestureRecognizer it 
       // Along with views 
       var otherClass = OtherClass()
       self.view.addSubview(otherClass.view)
       otherClass.bindGestures()
    } 
 }

Tapping will give you an error.

To fix it:

class ViewController : UIViewController {
    var otherClass : OtherClass!
    override func viewDidLoad() {
       super.viewDidLoad()

       self.otherClass = OtherClass()
       self.view.addSubview(self.otherClass.view)
       self.otherClass.bindGestures()
    } 
 }

Putting your object as a viewController's property solves the problem.

Unfortunately, your example is not comprehensive enough, but the idea is more or less clear. I would advise you playing with scopes.

user3677173
  • 2,559
  • 2
  • 17
  • 16
  • Interesting. The problem is the number of clickable images are generated at runtime. I am going through a for loop and adding the images (and their callbacks) dynamically. Do you know what the 'cause' of the problem is in the first example? Is it an engine bug? – Aggressor Oct 16 '14 at 16:14
  • In this case, put an array property to a class, and add it there. Again - the same issue, i have been struggling as well. If it defined within a method - that's probably an issue. – user3677173 Oct 17 '14 at 09:39
  • Even if they are stored in an array the crash happens. I think it's an Xcode level bug at this point – Aggressor Oct 17 '14 at 16:42
  • You should try changing the approach. Try avoiding callbacks. Create an array, put it as a class property. Populate it, adding tap gestures consecutively. Just as simple as possible. – user3677173 Oct 18 '14 at 18:57