Need advices

Hello,

I’m still scratching my head about how to use lock/unlock object especially in a plugin. That’s why I’m wondering if this is the best way to do it. Thanks for your insight.

static void RaiseFannException(char const *msg)
{
    REALobject exc = REALnewInstanceOfClass(&xFANNException_Definition);
    if (exc != NULL) {
        REALstring errStr = REALBuildString(msg, strlen(msg) + 1, kREALTextEncodingUTF8);
        if (errStr != NULL) {
            REALSetPropValueString(exc, "Message", errStr);
            REALUnlockString(errStr);
            REALRaiseException(exc);
            REALUnlockObject(exc); // Before or after raising the exception ??
        }
    }
}

After of course. Otherwise the object is destroyed before calling REALRaiseException.

Thanks Christian.

Isn’t it better to do it like this :

static void RaiseFannException(char const *msg)
{
    REALobject exc = REALnewInstanceOfClass(&xFANNException_Definition);
    if (exc != NULL) {
        REALstring errStr = REALBuildString(msg, strlen(msg) + 1, kREALTextEncodingUTF8);
        if (errStr != NULL) {
            REALSetPropValueString(exc, "Message", errStr);
            REALUnlockString(errStr);
            REALRaiseException(exc);
        }
        REALUnlockObject(exc);
    }
}

Looks good to me.

I always run the Constructor of the base class.

Since without running the constructor then the behavior is a bit undefined.

Just creating new instance of class with REALnewInstanceOfClass runs none of the constructors, but does run a initializer.

So my template is something like this:

void ThrowSVGException(const char* message, RBInteger errorCode)
{
    REALobject exceptionInstance = REALnewInstanceOfClass(&SVGExceptionClass);

    void (*RuntimeException_Constructor)(REALobject instance, REALstring message, RBInteger errorCode) =
        (void (*)(REALobject, REALstring, RBInteger)) REALLoadObjectMethod(exceptionInstance, "Constructor(message as String, errorCode as Integer)");

    REALstring errorMessage = REALBuildStringWithEncoding((const char*)message, (int)strlen(message), kREALTextEncodingUTF8);

    RuntimeException_Constructor(exceptionInstance, errorMessage, errorCode);

    REALUnlockString(errorMessage);

    REALRaiseException(exceptionInstance);

    REALUnlockObject(exceptionInstance);
}

And your strlen dont need the +1 there.

Thanks Björn for the tip.

You’re absolutely right. strlen doesn’t need a +1. I added this probably because I thought it was necessary to reserve space for a termination.