jamoma / JamomaCore

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

Can deleting an object throw an exception? #171

Closed nwolek closed 10 years ago

nwolek commented 10 years ago

Noticing that in the new object instantiation syntax, I need to catch exceptions like this:

// instantiate a matrix for testing
TTMatrixPtr matrix = NULL;
try {
    matrix = new TTMatrix(kTTSymEmpty);
    TTTestLog("TTMatrix matrix instantiates successfully");

} catch (...) {
    TTTestLog("TTMatrix matrix did NOT instantiate");
    return kTTErrInstantiateFailed;
}

But when I delete an object, I just do this:

delete matrix;

Is there a possibility of an uncaught exception out of the delete command? Before we had to do this:

TTErr err = TTObjectBaseRelease((TTObjectBasePtr*)&matrix);

To be clear: the compiler does not complain. This is more me being curious about why one has the potential for errors and the other does not.

tap commented 10 years ago

In the absence of documentation, you can look to see if any of the code you execute uses a "throw" statement. The TTObject constructor, for example, contains this nugget: https://github.com/jamoma/JamomaCore/blob/master/Foundation/library/includes/TTObject.h#L46

The destructor throws no such exceptions, unless there is one buried inside of the function that it calls, etc.

nwolek commented 10 years ago

Thanks for the clear explanation via that nugget. Should we mirror that code in the destructor so that it DOES throw an exception? It seems like a net loss if we used to get error reporting here, but now we don't. Then again, maybe it was never an issue on deletion.

tap commented 10 years ago

The question is, did ever actually return an error or did it always just return zero?

--••• •••--

On Wed, Nov 13, 2013 at 7:50 AM, nwolek notifications@github.com wrote:

Thanks for the clear explanation via that nugget. Should we mirror that code in the destructor so that it DOES throw an exception? It seems like a net loss if we used to get error reporting here, but now we don't. Then again, maybe it was never an issue on deletion.

— Reply to this email directly or view it on GitHubhttps://github.com/jamoma/JamomaCore/issues/171#issuecomment-28395175 .

nwolek commented 10 years ago

I followed the trail...

TTObject destructor calls ttEnvironment->releaseInstance releaseInstance currently always returns kTTErrNone BUT

Thinking this through some more: TTObject is effectively passing a version of itself to the TTEnvironment when it calls the releaseInstance method. If it were invalid, it would not be able to call the method in the first place. So it makes sense that errors would happen during constructor but not destructor.

OK, I am closing this question.