Releasing an external's data

Oct 24, 2012 at 5:24am

Releasing an external's data

I’m wondering whether there’s any method that gets called when an external is deleted? The reason I ask is that I’d like to integrate some Obj-C objects into my externals — some of the collection classes would be very handy in Max — but I can’t find any way of knowing when the last instance of an Obj-C object should be released. Is there no method similar to Obj-C’s -release: method? Or, is there any other way for me to find out when an external is deleted (maybe some sort of notification)?

J.

#64897
Oct 24, 2012 at 5:46am

erp… sorry… figured it out. I didn’t assign a free method when I instantiated the object, so myObj_free() wasn’t getting called… duh.

#234005
Oct 24, 2012 at 5:56am

Come to think of it, is there any “best practice” when implementing class_free() in an external – something similar to [super release] in obj-c?? I’ve seen sample code that doesn’t implement class_free() at all, so I’m assuming the runtime has a default method, for cases where it hasn’t been implemented. But probably we have to handle this manually once we implement it, yes?

#234006
Oct 24, 2012 at 6:13am

HI,

AFAIK, classes are not “released” until MaxMSP quit and so most of time you doesn’t need to care about that ; but in case you have to, you can use quittask_install( ) from the SDK (in Miscellaneous) or try a function marked with __attribute__((destructor)).

HTH.

#234007
Oct 24, 2012 at 9:54am
#234008
Oct 24, 2012 at 4:38pm

Thanks Nicolas. But this isn’t quite the answer I was looking for. In obj-c, when you override -dealloc: you are responsible for making sure everything that would have happened still happens. That’s why the common practice is to include a [super dealloc] (sorry, I said [super release] above, which was wrong), to be sure that an object’s supercalss releases any objects that it created. I was just wondering whether implementing class_free() carried a similar responsibility – that is, do I have to call some particular method to ensure that the object’s data is freed. As I mentioned earlier, I want to release the obj-c objects (well, pointers) I include in my Max object’s struct, so I’m doing something like this (hacked into the plussz sample code):

void plussz_free(t_plussz *x)
{
[x->dict release]; // free plussz’s NSMutableDictionary before the Max object is gone!
}

This just makes sure the NSMutableDictionary “dict” gets released (which couldn’t happen otherwise), but it doesn’t do anything about the struct’s other fields.
I tried sysmem_freeptr() and object_free(), the latter of which crashed Max. Since the object is created in plussz_new() using object_alloc(), then object_free() should be the correct method, according to the docs… So, I’m guessing the crash is telling me not to mess around with freeing the struct…

#234009
Oct 24, 2012 at 5:24pm

Hi,

No, you do not have to implement a super-like call. In the myobject_free(t_myobject *x) method you just have to care about dynamic allocations you have done, nothing more. You are not responsible to freed the “under the hood” structures of the object.

But take care to not confuse the class and the instance. The class is loaded only once and released when MaxMSP quit. Each object in the patch is an instance of the class.

From http://cycling74.com/forums/topic.php?id=14188

void flop_free(t_flop *x)
{
    if (x->m_qelem) {
        qelem_unset(x->m_qelem);
        qelem_free(x->m_qelem);
    }

    [x->flopWindow setDelegate:nil];
    [x->flopWindow setFlopObject:NULL];
    [x->flopWindow performClose:nil];
    [x->flopWindow release];

    [x->flopWindowDelegate release];
    [x->flopView release];
}

PS : i’m not very used with objective-C so i hope there is no error ;-)

#234010

You must be logged in to reply to this topic.