- Home /
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( wheelTransform.name + " 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;
else
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( wheelTransform.name + " Steer Column" );
go.transform.position = wheelTransform.position;
go.transform.rotation = wheelTransform.rotation;
go.transform.parent = transform;
wheelTransform.parent = go.transform;
}
else
wheel.driveWheel = true;
return wheel;
}
Answer by Mike 3 · Nov 24, 2010 at 05:07 PM
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
Answer by Love N · Nov 27, 2010 at 12:34 PM
Ah, I see. That makes life incredibly easy :)
Thanks Mike. WBR // Love
Your answer
![](https://koobas.hobune.stream/wayback/20220613084356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rear wheels move to the center of the car object in car tutorial 1 Answer
What is the best way to modify the Car Tutorial for more cars? 2 Answers
Assets never unloading after loading next scene 1 Answer
200MB file save causing unity memory to increase by over a gigabyte 1 Answer
How do I avoid memory leaks when setting UI.Text.text? 0 Answers