C
Chris Berg
My Java application is handling a lot of images. For some of the
images I need to load them completely before handlng them, so I use a
MediaTracker. Here is my utility method for that purpose:
private static MediaTracker mt = new MediaTracker(new Canvas());
private static int mtID;
// return 0 if OK
public static int trackImage(Image img) {
if (img == null) {
return 16;
}
try{ // .. finally
try {
mt.addImage(img, ++mtID);
mt.waitForID(mtID, 5000);
} catch (Throwable e) {
return 16;
}
int r = mt.statusID(mtID, true);
if ( (r & MediaTracker.COMPLETE)!=0) {
return 0;
} else {
return r;
}
}finally{
mt.removeImage(img,mtID);
}
}
My concern is, do I really get everything properly cleaned-up? or is
the MediaTracker at risk of getting filled up with images, eventually
causing a memory leak?
Is it OK to re-use the MediaTracker this way? All the code examples
I've seen, creates a new instance with every image, and that seems to
me to be a waste of ressources. Or?
Chris
images I need to load them completely before handlng them, so I use a
MediaTracker. Here is my utility method for that purpose:
private static MediaTracker mt = new MediaTracker(new Canvas());
private static int mtID;
// return 0 if OK
public static int trackImage(Image img) {
if (img == null) {
return 16;
}
try{ // .. finally
try {
mt.addImage(img, ++mtID);
mt.waitForID(mtID, 5000);
} catch (Throwable e) {
return 16;
}
int r = mt.statusID(mtID, true);
if ( (r & MediaTracker.COMPLETE)!=0) {
return 0;
} else {
return r;
}
}finally{
mt.removeImage(img,mtID);
}
}
My concern is, do I really get everything properly cleaned-up? or is
the MediaTracker at risk of getting filled up with images, eventually
causing a memory leak?
Is it OK to re-use the MediaTracker this way? All the code examples
I've seen, creates a new instance with every image, and that seems to
me to be a waste of ressources. Or?
Chris