Vala closure reference cycle - glib

I'm writing a class in Vala where I bind two of the same object's properties together, with a closure to transform one to the other.
class Foo : Object
{
public int num { get; set; }
public int scale = 2;
public int result { get; set; }
construct {
this.bind_property("num", this, "result", 0,
(binding, src, ref dst) => {
var a = src.get_int();
var b = this.scale;
var c = a * b;
dst.set_int(c);
}
);
}
}
The closure keeps a reference this (because I use this.scale), which creates a reference cycle which is keeping my class alive even when all other references to it are lost.
The binding will only be removed when the reference count reaches 0, but refcount will only reach 0 when the biding and its closure is removed.
Is there a way to make the closure's reference to this weak? Or to probe when the refcount reaches 1 in order to remove it?

Not tested but can you assign this to a weak variable and reference that in the closure instead? E.g.:
weak Foo weak_this = this;
this.bind_property(…, (…) => {
…
b = weak_this.scale;
…
}

This is a known defficiency of the Vala compiler and is discussed in this issue.
For now, the problem can be avoided by doing the binding from a static method where the closure does not capture this.
class Foo : Object
{
public int num { get; set; }
public int scale = 2;
public int result { get; set; }
construct {
this.bind_properties(this);
}
private static void bind_properties(Foo this_)
{
weak Foo weak_this = this_;
this_.bind_property("num", this_, "result", 0,
(binding, src, ref dst) => {
var a = src.get_int();
var b = weak_this.scale;
var c = a * b;
dst.set_int(c);
}
);
}
}

Related

Dependency injection issue (bidirectional communication)

I have a an instance of A and b an instance of B
a must be able to call a method on b and b must then immediatly call a method on a if some checks pass.
To achieve this I would have cyclic DI
public A(B b) { _b = b; }
public void CallToB() { _b.Method(); }
public void Method() { DoSomething(); }
public B(A a) { _a = a; }
public void Method() { if (SomeCheck()) _a.Method(); }
I know I could get arround this, using events and let b be unaware/independant of a. But it would feel wrong.
Note: I haven't seen an answer to this question where bidirectional communication was made possible.
You can solve this issue by depending on interfaces instead of concrete types and then use property injection. Here is an example:
public interface IA
{
void Method();
}
public class A : IA
{
private readonly IB _b;
public A(IB b){_b = b;}
//...
}
public interface IB
{
void Method();
}
public class B : IB
{
private readonly IA _a;
public B(IA a){_a = a;}
//...
}
public class BCycleDependencyBreaker : IB
{
private IB _b;
public IB b
{
set { _b = value; }
}
public void Method()
{
_b.Method();
}
}
You then use BCycleDependencyBreaker when you compose like this:
var b_cycle_dependency_breaker = new BCycleDependencyBreaker();
//Make a depend on this implementation of b that currently does nothing
A a = new A(b_cycle_dependency_breaker);
//Make b depend on a
B b = new B(a);
//Now, let the proxy implementation delegate calls to the real b
b_cycle_dependency_breaker.b = b;

How to create a thread safe singleton in vala?

I want to create a thread-safe singleton instance for my vala class.
As you know, singletons may lead to threading issues if not properly implemented.
Also you can use SingleInstance Code Attribute. It does the same for you automatically!
[SingleInstance]
public class ExampleClass : Object {
public int prop { get; set; default = 42; }
public ExampleClass () {
// ...
}
}
int main (string[] args) {
var a = new ExampleClass (); // the two refs
var b = new ExampleClass (); // are the same
b.prop += 1;
assert (a.prop == b.prop);
return 0;
}
Note, that in this case you don't need to call a static function like instance() or get_instance(). Simply creating an object via new will give you a reference to the singleton.
The recommended way is to use the GLib.Once construct:
public class MyClass : Object {
private static GLib.Once<MyClass> _instance;
public static unowned MyClass instance () {
return _instance.once (() => { return new MyClass (); });
}
}

Dart: How to convert variable identifier names to strings only for variables of a certain type

Using Dart here.
As the above title suggests, I have a class (shown below) that has three bool instance variables. What I want to do is create a function that inspects the identifier names of these instance variables and prints each of them out in a string. The .declarations getter that comes with the ClassMirror class ALMOST does this, except it also gives me the name of the Constructor and any other methods I have in the class. This is no good. So really what I want is a way to filter by type (i.e., only give me the boolean identifiers as strings.) Any way to do this?
class BooleanHolder {
bool isMarried = false;
bool isBoard2 = false;
bool isBoard3 = false;
List<bool> boolCollection;
BooleanHolder() {
}
void boolsToStrings() {
ClassMirror cm = reflectClass(BooleanHolder);
Map<Symbol, DeclarationMirror> map = cm.declarations;
for (DeclarationMirror dm in map.values) {
print(MirrorSystem.getName(dm.simpleName));
}
}
}
OUTPUT IS:
isMarried
isBoard2
isBoard3
boolsToStrings
BooleanHolder
Sample code.
import "dart:mirrors";
void main() {
var type = reflectType(Foo);
var found = filter(type, [reflectType(bool), reflectType(int)]);
for(var element in found) {
var name = MirrorSystem.getName(element.simpleName);
print(name);
}
}
List<VariableMirror> filter(TypeMirror owner, List<TypeMirror> types) {
var result = new List<VariableMirror>();
if (owner is ClassMirror) {
for (var declaration in owner.declarations.values) {
if (declaration is VariableMirror) {
var declaredType = declaration.type;
for (var type in types) {
if (declaredType.isSubtypeOf(type)) {
result.add(declaration);
}
}
}
}
}
return result;
}
class Foo {
bool bool1 = true;
bool bool2;
int int1;
int int2;
String string1;
String string2;
}
Output:
bool1
bool2
int1
int2

1067: Implicit Coercion of a value

So I'm currently trying to run my collision test on my two sprites, and I'm getting the following error:
C:\\Code\\Game\Game.as, Line 54, Column 34 1067: Implicit coercion of a value of type mvc:PlayerModel to an unrelated type assets.Scripts:SpriteAnimation.
Which is pointing to the following line of code:
handleSpriteToSpriteCollision(_player, _boss);
The function is as follows:
private function handleSpriteToSpriteCollision(sprite1:SpriteAnimation, sprite2:SpriteAnimation):void
{
var toSprite2 : VectorModel = new
VectorModel(0,0,0,0, sprite2.x - sprite1.x, sprite2.y - sprite1.y);
var bitmapData1:BitmapData = new BitmapData(sprite1.width, sprite1.height);
while(testBitmapCollision(sprite1.spriteFrameBitmapData, sprite1.topLeftX, sprite1.topLeftY, sprite2.spriteFrameBitmapData, sprite2.topLeftX, sprite2.topLeftY))
{
sprite2.x -= toSprite2.dx;
sprite2.y -= toSprite2.dy;
}
}
Both sprites display just fine, but as soon as I make the function call it all comes crumbling down. At this point I just need some fresh eyes to take a look at the code to see what's going wrong.
Edit: Here is the PlayerModel.as
package mvc
{
import flash.events.Event;
import flash.events.EventDispatcher;
import assets.Scripts.SpriteAnimation;
public class PlayerModel extends EventDispatcher
{
private var _previousX:Number = 0;
private var _previousY:Number = 0;
private var _xPos:Number = 0;
private var _yPos:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
private var _height:uint = 30;
private var _width:uint;
private var _color:uint;
public function PlayerModel():void
{
}
public function update():void
{
xPos += vx;
yPos += vy;
}
public function get height():uint
{
return _height;
}
public function get color():uint
{
return _color;
}
public function get xPos():Number
{
return _xPos;
}
public function set xPos(value:Number):void
{
_xPos = value;
dispatchEvent(new Event(Event.CHANGE));
}
public function get yPos():Number
{
return _yPos;
}
public function set yPos(value:Number):void
{
_yPos = value;
dispatchEvent(new Event(Event.CHANGE));
}
public function set setX(value:Number):void
{
_previousX = value - vx;
xPos = value;
}
public function set setY(value:Number):void
{
_previousY = value - vy;
yPos = value;
}
}
}
The first thing i would check is the class of your boss and player objects inherit your SpriteAnimation class:
class PlayerModel extends SpriteAnimation
{ ...
Seems as though your handleSpriteToSpriteCollision function is looking for two SpriteAnimations and at least the PlayerModel being passed isn't one
Sprite inherits EventDispatcher so as long as your SpriteAnimation inherits Sprite as well, you shouldn't lose any functionality

ActionScript3: how to initialize array length in object after it has been declared?

I'm using an AS3 class that is:
package {
public class PeopleInfo {
public var elements:int;
public var PeopleName:Array;
public var PeopleInt:Array;
public var PeopleDecimal:Array;
}
}
In another file I've got:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
creationComplete="initApp()"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
public var ii:int;
public var dataWriteToDB:PeopleInfo = new PeopleInfo;
public function initApp():void
{
// Initialize data provider array.
var int_data:Array = new Array(10, 20, 30, 40, 50);
var decimal_data:Array = new Array(13.11, 23.34, 35.69, 43.29, 58.92);
var name:Array = new Array("Joe", "Karen", "Simon", "Greg", "Alice");
dataWriteToDB.elements = 5; // number of elements in array
for (ii = 0; ii < dataWriteToDB.elements; ii++)
{
dataWriteToDB.PeopleName[ii] = name[ii];
dataWriteToDB.PeopleInt[ii] = int_data[ii];
dataWriteToDB.PeopleDecimal[ii] = decimal_data[ii];
}
}
and so on...
I'm getting a run-time error: Error #1009: Cannot access a property of method of a null object reference referring to the for loop's first line dataWriteToDB.PeopleName since it is NULL.
I'm guessing the problem here is that while dataWriteToDB is declared initially, the array lengths for the arrays in PeopleInfo class have not been set yet. Or, not sure otherwise why it's NULL. Anyone know how to clear this up?
The arrays have not been initialized in the class. You should do it in the declaration:
package {
public class PeopleInfo {
public var elements:int;
public var PeopleName:Array = [];
public var PeopleInt:Array = [];
public var PeopleDecimal:Array = [];
}
}
Also consider using push to add elements to the array, to avoid accidentally accessing a non-existing index:
for (ii = 0; ii < dataWriteToDB.elements; ii++)
{
dataWriteToDB.PeopleName.push(name[ii]);
dataWriteToDB.PeopleInt.push(int_data[ii]);
dataWriteToDB.PeopleDecimal.push(decimal_data[ii]);
}

Resources