[ANN] weak_attr_accessor – gem that adds accessor which wraps objects with WeakRef, for RubyMotion

63 views
Skip to first unread message

Hwee-Boon Yar

unread,
Feb 1, 2015, 3:42:35 AM2/1/15
to rubym...@googlegroups.com
I'm pretty sure everyone has these few lines of code somewhere, but I had to use it for purplish-layout, so..


Hwee-Boon

Caram Dache

unread,
Feb 1, 2015, 7:34:19 AM2/1/15
to rubym...@googlegroups.com
Excellent !
Adopted already.

Caram

--
You received this message because you are subscribed to the Google Groups "RubyMotion - Ruby for iOS, OS X, and Android" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubymotion+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubymotion/58e8818b-87f1-46a2-a2c7-671f7a5695e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Caram Dache

unread,
Mar 2, 2015, 3:53:06 PM3/2/15
to rubym...@googlegroups.com
Hwee-Boon,

Any chance you might check if the WeakRef is not already a weak ref? I have this case where weak_attr_accessor is called twice in succession by Master, Slave1, Slave2 pattern. The app crashes when Slave2 tried to convert into a weak ref, a ref that was already made by weak by Slave1. And I cannot really turn the accessor in Slave2 into a non weak accessor, because Slave2 is also used Slave1b and Slave1b uses a strong ref instead.

Question: how does one check that a ref is weak in the first place?

Caram

Le 1 févr. 2015 à 09:42, Hwee-Boon Yar <hwee...@gmail.com> a écrit :

Hwee-Boon Yar

unread,
Mar 3, 2015, 12:20:59 AM3/3/15
to rubym...@googlegroups.com
Wrapping a WeakRef with another should work like you expect it to, i.e.

class A
  weak_attr_accessor
:w
end
class B
  weak_attr_accessor
:w
end
o
= NSObject.new
a
= A.new
b
= B.new
a
.w = o
b
.w = a.w


Do you have a short example where it wouldn't work correctly?

You can test for a WeakRef by doing:

a.respond_to? 'weakref_alive?'

Did you crash with a "WeakRef::RefError', reason: 'Invalid Reference - probably recycled (WeakRef::RefError)" message? WeakRef in RubyMotion (as well as Ruby) behaves different from a weak reference in Objective C. Zero-ed weak references raises an exception. I noticed that this behavoir is more desirable in my code — since RubyMotion works with iOS — I'm wondering if I should add a check to make it behave similarly.. Thoughts?

Hwee-Boon

Caram Dache

unread,
Mar 5, 2015, 4:28:25 PM3/5/15
to rubym...@googlegroups.com
> Wrapping a WeakRef with another should work

You are right indeed.

> Did you crash with a "WeakRef::RefError', reason: 'Invalid Reference - probably recycled (WeakRef::RefError)" message?

Yeah indeed!

> I'm wondering if I should add a check to make it behave similarly.. Thoughts?

Sorry I don’t understand this part. The current crashing behavior seems fine. What did you have in mind exactly?

Caram
Reply all
Reply to author
Forward
0 new messages