We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Recycling previously named objects
Page Index Toggle Pages: 1
Recycling previously named objects (Read 735 times)
Recycling previously named objects
May 29th, 2009, 7:08pm
 
At the top of my sketch, I name an object that will be called from various other places/methods:

Code:
MovieMaker movie; 



I don't instantiate the object yet, not even in setup(), because I won't have the information I need for that until later (say, 30 seconds after the sketch starts running).  So, at a certain point of time, I have a method that gathers the right info and instantiates the object:

Code:
movie = new MovieMaker(applet, width, height, "video" + currentRecordingID + ".mov", 15, MovieMaker.ANIMATION, MovieMaker.HIGH); 



Then I record frames for my movie object, and eventually I call:

Code:
movie.finish(); 



The thing is, I want to reuse this object called "movie" again and again, to make new recordings.  I don't want to use an array of MovieMaker objects, for various reasons.  So my question is, what is the best way to delete/overwrite/recycle an existing, named object ("movie") re-instantiating it, as though it were a new object?

I have tried simply instantiating again, as in "movie = new MovieMaker(...)", but then got this error, which may or may not be related:

Code:
java(442,0xa041e720) malloc: *** error for object 0x42f34490: double free
*** set a breakpoint in malloc_error_break to debug


Is there a way to "clear out" an object, or un-instantiate it?  Thanks!
Re: Recycling previously named objects
Reply #1 - May 30th, 2009, 12:50am
 
movie=new MovieMaker(...) should have worked... if not try movie=null; first, and then movie=new MovieMaker(...)
Re: Recycling previously named objects
Reply #2 - May 30th, 2009, 1:46am
 
Looks like an error in the QT runtime (C language error).

Looking at MovieMaker.java, I see a dispose() function which does the finish() but also a QTSession.close(). Since the QTSession is created when doing new MovieMaker, it might be the way to go.
Re: Recycling previously named objects
Reply #3 - May 30th, 2009, 8:27am
 
Thanks, guys.  The error is inconsistent, but I'll keep trying to reproduce it and see what resolves it.

In terms of objects generally, though, is "objectName = null" an accepted way to kill/delete/uninstantiate an object?

Also, should that even be necessary, or should "objectName = new Object..." always be safe?

I'm just trying to figure out what others do in this situation, and what the accepted practice is.  Thanks!
Re: Recycling previously named objects
Reply #4 - May 30th, 2009, 9:05am
 
In general, the = new Object is enough, the old one looses its reference so is a candidate for garbage collection (GC).
The = null is rarely used but might be a hint if you no longer plan to use this variable and want quicker GC. It should not be useful if you reuse the var.
Re: Recycling previously named objects
Reply #5 - May 30th, 2009, 9:38am
 
Cool, thanks.  Good to better understand what's going on under the hood.
Page Index Toggle Pages: 1