[flexcoders] Re: Parameter passing !! I am shocked
AFAIK, in object oriented languages, an object itself is never destroyed setting to null a pointer that refers to it (and it doesn't get overwritten when assigning that pointer to another instance). In some OOL (C++) you have to make an explicit call to destroy in order to free the memory allocated by an object. This can lead to memory leaks if pointers are reused / go out of scope without calling destroy. Mordern languages (Java, .NET, ActionScript) are usually garbage-collection based, so you don't need to destroy anything.. a garbage collector is automatically executed in background by the VM, tracing object instances that are not referenced by any pointer yet, and destroying them. Bye Cosma --- In flexcoders@yahoogroups.com, Ravi Kumar Gummadi [EMAIL PROTECTED] wrote: Hi all How does flex handle parameter passing, as I understand, it is by Pass by reference. But I was having some memory leaks and playing around a few tweaks and the following snippet completely took me by surprise. // CODE START script public var obj:Object; createObject(){ obj = {test:1234,test2:5678}; } checkObject(objParam:Object){ trace(objParam); } deleteObject(objParam:Object){ objParam = null; // I tried objParam = undefined as well } /script mx:Button id=create click=createObject() / mx:Button id=check click=checkObject(obj) / mx:Button id=delete click=deleteObject(obj) / // END Now 1. I created the object by click on create 2. Then check for the existence of it (Traces [object object].. This is fine) 3. Then clicked delete. 4. Then again click on check... (Traces [object] [object] ! But since its passed by reference it should be NULL) Somewhere I read that it setting to NULL changes the reference count, does that mean in each of my function I need to set all the params to null at the end of it so that refercne count is reduced and raady for garbage collection!! ( That doesn't quite a sense !!) Regards Ravi
Re: [flexcoders] Re: Parameter passing !! I am shocked
When you set it to null it removes the reference the current scope or context, and when you pass it into that function it is in a different scope from this. had you done this.obj = null, then it would have been completely de-referenced and eligible for garbage collection. --- In flexcoders@yahoogroups.com, Ravi Kumar Gummadi [EMAIL PROTECTED] wrote: Hi all How does flex handle parameter passing, as I understand, it is by Pass by reference. But I was having some memory leaks and playing around a few tweaks and the following snippet completely took me by surprise. // CODE START script public var obj:Object; createObject(){ obj = {test:1234,test2:5678}; } checkObject(objParam:Object){ trace(objParam); } deleteObject(objParam:Object){ objParam = null; // I tried objParam = undefined as well } /script mx:Button id=create click=createObject() / mx:Button id=check click=checkObject(obj) / mx:Button id=delete click=deleteObject(obj) / // END Now 1. I created the object by click on create 2. Then check for the existence of it (Traces [object object].. This is fine) 3. Then clicked delete. 4. Then again click on check... (Traces [object] [object] ! But since its passed by reference it should be NULL) Somewhere I read that it setting to NULL changes the reference count, does that mean in each of my function I need to set all the params to null at the end of it so that refercne count is reduced and raady for garbage collection!! ( That doesn't quite a sense !!) Regards Ravi
[flexcoders] Re: Parameter passing !! I am shocked
When it is said that the object is passed by reference versus passed by value, what is being implied is that a copy of the object is NOT being sent, but a reference instead is sent. The reference itself is STILL contained within a variable scoped to the method (the parameter). Therefore, nulling out the reference from within deleteObject() has no effect on the obj var which still contains a reference to the actual object. The reference count should make sense if you look at it like this: When you create the object and assign it to the obj var, you have a reference count of 1. When you pass the object into the deleteObject() method, you now have a NEW reference (the parameter), so your reference count is now 2. When you null out the parameter, your reference count goes back down to 1 (the obj var is still in scope and still has a reference). Even if you didn't null out the parameter, the reference count would go back down to 1 when the deleteObject() method ends and the parameter goes out of scope. Bill --- In flexcoders@yahoogroups.com, Jeffry Houser [EMAIL PROTECTED] wrote: I think you missed his point. He sent in obj as a parameter to a function. So objParam should be a reference to obj; and nulling one should definitely null the other. I do notice that the functions in the example are not declared as functions with the public function keywords. Could that be a contributing factor to the problem? Roman Protsiuk wrote: objParam is not the same reference to created object as obj. Whenever you set objParam to null it becomes null (try checking it out in your deleteObject method). But who said the obj should become null? To prepare obj for garbage collection you should set obj to null. R. On 6/7/07, *Ravi Kumar Gummadi* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Hi all How does flex handle parameter passing, as I understand, it is by Pass by reference. But I was having some memory leaks and playing around a few tweaks and the following snippet completely took me by surprise. // CODE START script public var obj:Object; createObject(){ obj = {test:1234,test2:5678}; } checkObject(objParam:Object){ trace(objParam); } deleteObject(objParam:Object){ objParam = null; // I tried objParam = undefined as well } /script mx:Button id=create click=createObject() / mx:Button id=check click=checkObject(obj) / mx:Button id=delete click=deleteObject(obj) / // END Now 1. I created the object by click on create 2. Then check for the existence of it (Traces [object object].. This is fine) 3. Then clicked delete. 4. Then again click on check... (Traces [object] [object] ! But since its passed by reference it should be NULL) Somewhere I read that it setting to NULL changes the reference count, does that mean in each of my function I need to set all the params to null at the end of it so that refercne count is reduced and raady for garbage collection!! ( That doesn't quite a sense !!) Regards Ravi -- Jeffry Houser, Technical Entrepreneur, Software Developer, Author, Recording Engineer AIM: Reboog711 | Phone: 1-203-379-0773 -- My Company: http://www.dot-com-it.com My Podcast: http://www.theflexshow.com My Blog: http://www.jeffryhouser.com