jamoma / JamomaCore

Jamoma Frameworks for Audio and Control Structure
Other
36 stars 14 forks source link

When is it necessary to delete/release a TTAudioObject? #128

Closed nwolek closed 11 years ago

nwolek commented 11 years ago

In looking over Tim's syntax recommendations from this week, I am a little confused about when it is necessary to delete a created object (formerly TTObjectRelease). The new delete syntax is demonstrated here: https://github.com/jamoma/JamomaDoc/blob/master/Doxygen/intro_chapters/30-UsingObjects.dox#L45

Tim mentioned TTLimiter could be looked at as an example. This code uses 2 TTAudioObject for a dcBlocker and preamp. Looking at the destructors, in the master branch you have the old syntax: https://github.com/jamoma/JamomaCore/blob/master/DSP/extensions/EffectsLib/sources/TTLimiter.cpp#L58

But in the dev branch, there is no corresponding "delete" for these objects: https://github.com/jamoma/JamomaCore/blob/dev/DSP/extensions/EffectsLib/sources/TTLimiter.cpp#L62

Is it no longer necessary? I can tell you that when I try to delete inside a destructor, it fails to build.

tap commented 11 years ago

If the member is a pointer, then that means it is just a pointer to some memory somewhere (obviously). In this case you must create an object and assign it to the pointer

TTAudioObject* aPointer = NULL;
// later on...
aPointer = new TTAudioObject("limiter");

In this case (you created the object using the new operator) you must delete it using the delete operator.

delete aPointer;

On the other hand, you can create TTAudioObject on the stack or, in more human terms, you can create it "directly" (not creating it separately and then manually assigning it to some pointer):

TTAudioObject myObject("limiter");

Now you have "myObject" which is an actual object (not a pointer an object, but the actual thing). When this goes out of scope it will be popped from the stack (freed). It's like a function that has local variables in it for ints and floats -- you don't manually free those, they are freed when you return from the function. It's the same with C++ objects.

Now in the case of TTLimiter including a gain object, this scenario is slight obfuscated. In the definition of the class structure we tell it that we have a TTAudioObject (not a pointer anymore, which is why we don't manually free it). But the initialization of any classes contained in another class must be done using initializers to the constructor. So TTAudioObject for the gain instance is initialized in TTLimiter's constructor (well, to be technical, not in the constructor but as an initializer prior to the execution of the constructor).

Does this help? Should we have an FAQ in the Doxygen documentation? Should this be one of the items in such an FAQ?

nwolek commented 11 years ago

OK, I see the difference now. The old syntax was consistently using a pointer and therefore needed the instantiate and release. Your connection with local variables and the way they are popped from the stack helps.

+1 for a FAQ, although it would be rather lame to have just 1 question. I will open an issue in JamomaDoc, link to this issue in JamomaCore and we can sit on it until there are maybe 4 or 5 questions?

nwolek commented 11 years ago

done -- https://github.com/jamoma/JamomaDoc/issues/2