Memory leak in Car Tutorial ?

Hi Folks,

In the `SetupWheel` function of the Car.js script there is what seems to me like a leak.

The GameObject variable go is reused without having been assigned anywhere.

In a third gen language like C/C++, this would certainly cause a memory leak.

A `WheelCollider` `wc` is added to the `go` object, and assigned to the `Wheel` instance,

but go itself is not assigned to anything. When (if?) wheel is deleted, it’s collider wc

will get orphaned unless Wheel deletes it, so You’ll leak a WheelCollider, and it’s owner

(the previous go) will get orphaned even if wc is deleted, and You’ll leak a GameObject.

However, I'm a C++ programmer and quite new to scripting systems,

so I might be missing some fine point about UnityScript.

Could anyone please throw some light on this?

Is it a leak (as it seems to me), or does it rely on some built in

safety mechanism of the scripting language to save the day?

I’m learning UnityScript, so some enlightenment would be very appreciated.

Thanks and Best Regards

// Love N

[ Here's the function, with my remarks on the spot ]

function SetupWheel( wheelTransform : Transform, isFrontWheel : boolean )
    var go : GameObject = new GameObject( + " Collider" );
    go.transform.position = wheelTransform.position;
    go.transform.parent   = transform;
    go.transform.rotation = wheelTransform.rotation;

    var wc : WheelCollider = go.AddComponent( typeof( WheelCollider )) as WheelCollider;
    wc.suspensionDistance = suspensionRange;

    var js : JointSpring = wc.suspensionSpring;
    if (isFrontWheel)
        js.spring = suspensionSpringFront;
        js.spring = suspensionSpringRear;

    js.damper = suspensionDamper;
    wc.suspensionSpring = js;

    wheel = new Wheel(); 
    wheel.collider = wc;
    wc.sidewaysFriction = wfc;
    wheel.wheelGraphic = wheelTransform;
    wheel.tireGraphic = wheelTransform.GetComponentsInChildren( Transform )[1];

    wheelRadius = wheel.tireGraphic.renderer.bounds.size.y / 2; 
    wheel.collider.radius = wheelRadius;

    if (isFrontWheel)
        wheel.steerWheel = true;

        // 'go' is reused without previous object having been assigned to anything.
        // This looks like a classic leak to a driven C++ programmer like me.
        // What's the deal here ?

        go = new GameObject( + " Steer Column" );
        go.transform.position = wheelTransform.position;
        go.transform.rotation = wheelTransform.rotation;
        go.transform.parent = transform;
        wheelTransform.parent = go.transform;
        wheel.driveWheel = true;

    return wheel;

It doesn't make any difference, no - the engine is the one keeping the references to the new objects (including the native c++ objects created behind the scenes), so when it needs deleting, it'll be done at that end

Ah, I see. That makes life incredibly easy :)

Thanks Mike. WBR // Love