Question about WaveData in lwjgl.util

Started by Cornix, September 30, 2014, 17:50:02

Previous topic - Next topic

Cornix

Hi there,

I was recently browsing the source code of the WaveData class in the lwjgl.util.jar where I read the following code:
public class WaveData {
	/** actual wave data */
	public final ByteBuffer data;

	/**
	* Disposes the wavedata
	*/
	public void dispose() {
		data.clear();
	}
// much more...
}

But as far as I understand the "clear" method of the ByteBuffer does nothing but change the position, limit and mark of the Buffer. So the dispose method does effectively nothing, correct?

Why is it even there then? Is this a mistake, or am I overlooking something?

Thanks in advance.

quew8

Not that I know, this is just guessing:

There isn't really a way to properly dispose of objects (interpreted as "free the used memory") in Java. As I expect you know, you have to have no active references to them and then the gc will take care of it in it's next sweep. The absolute best you can do is dispose of all references and then ask the gc to collect, which is a bad idea unless you know exactly what you are doing (or so I'm told, far too scared to try it myself). So there is no way this method could actually dispose of the data.

What this method does do, is to give you a way of documenting in your code that this WaveData is done with and you shouldn't be using it any more. Which if you ask my opinion is quite useful. It's like marking a variable as private, it's only real purpose is to help the programmer out, it doesn't actually do anything in compiled code (OK reflection, that is kind of untrue but you get the picture).

Perhaps this method would be more effective if it assigned "null" to "data" as this would encourage Java to get rid of it and any attempt to use the WaveData instance after dispose() had been called would then fail. Potentially this is even more useful. Or maybe not, I leave it to smarter people to decide.

Cornix

Quote from: quew8 on October 04, 2014, 18:36:25What this method does do, is to give you a way of documenting in your code that this WaveData is done with and you shouldn't be using it any more. Which if you ask my opinion is quite useful. It's like marking a variable as private, it's only real purpose is to help the programmer out, it doesn't actually do anything in compiled code (OK reflection, that is kind of untrue but you get the picture).

But that is not really what the clear() method of ByteBuffer does. It just resets the position and limit. Its a useful method you can use very often, but I dont see why you would do that on WaveData except perhaps if you wished to upload it to OpenAL twice. But for that purposes the name sounds badly choosen in my opinion.

I was just curious why this method exists, it seems like its implementation is contradicting its documentary.

quew8

I said that about Wavedata.dispose(), not Buffer.clear(). But I agree that it's documentation is somewhat contradictory. You could push an update to the documentation which would feed through to the nightlies but I don't think there is going to another official 2.9 release so better to make sure it isn't in the 3.0 release (I know utils are being cut down on but I doubt WaveData will go, however it might have already been rewritten).

Cornix

I personally dont care too much about it, there is no need to actually use this function so there is no harm done if it exists.
I just felt like pointing it out.