Class instance problem


    May 28 2008 | 1:08 pm
    Hi,
    I have created a max external that works fine on it's own but when there are 2 instances of the external the behaviour is very strange.
    I think I know why it is but I can't fix it. I have a globally defined class instance that is required by each instance of the external. So I think memory resources are being shared when there should be 2 separate instances of the class.
    My struct definition looks like this:
    typedef struct { t_pxobject msp_data; void* root; void* type; void* features;
    } MyObject;
    ChordDetect chord;
    and you can see the class instance 'chord' below it. I have tried placing the class instance inside the struct but this just causes max to crash.
    Where should I place this to have 2 separate objects operating without problems?
    I hope I have explained properly and many thanks in advance for any help,
    Adam

    • May 28 2008 | 1:22 pm
      Adam Stark wrote: > My struct definition looks like this: > > typedef struct { > t_pxobject msp_data; > void* root; > void* type; > void* features; > > } MyObject; > > ChordDetect chord; > > and you can see the class instance 'chord' below it. I have tried placing the class instance inside the struct but this just causes max to crash.
      It should go in the object struct if you want each object to have it's own. Problem is that your struct is too large then. On object creation Max allocates memory for the struct, but it's limited to about 32k (I think in Max5 that will change). So when you place something really large in the struct Max crashes there. Instead use a pointer to the ChordDetect thing and allocate it separately once your object has been created (and free it again when your object gets deleted).
      And a personal hint: have a look at [at.chord], maybe it already does what you need: http://artificial-tango.nullmedium.de/
      Olaf
    • May 28 2008 | 2:02 pm
      typedef struct tHugeComponent { // some huge stuff in here };
      typedef struct tHugeObject { t_pxobject coreObject; // whatever else you need
      tHugeComponent* humongous; // a pointer!!! };
      void NewHugeObject(/* whatever */) { tHugeObject* x = NULL; // Start with null for safety
      // standard setup & instantiation stuff // including allocation of x
      x->humongous = (tHugeComponent*) malloc(sizeof(tHugeComponent));
      // Use malloc(), not getbytes() // malloc() has no 32kB limit // and your new method will not be // called from the high priority thread
      // But you might want to test for malloc() returning NULL
      return x; }
      Untested code.
    • May 28 2008 | 2:43 pm
      Thanks for all the help! I think I have something working now!
      Adam
    • May 28 2008 | 3:21 pm
      On 2008 May 28, at 8:22 AM, Olaf Matthes wrote:
      > Adam Stark wrote: >> My struct definition looks like this: >> typedef struct { >> t_pxobject msp_data; >> void* root; >> void* type; >> void* features; >> } MyObject; >> ChordDetect chord; >> and you can see the class instance 'chord' below it. I have tried >> placing the class instance inside the struct but this just causes >> max to crash. > > It should go in the object struct if you want each object to have > it's own. Problem is that your struct is too large then. On object > creation Max allocates memory for the struct, but it's limited to > about 32k (I think in Max5 that will change). So when you place > something really large in the struct Max crashes there. Instead use > a pointer to the ChordDetect thing and allocate it separately once > your object has been created (and free it again when your object > gets deleted).
      Currently, and this is still the case in Max 5, there are two parallel APIs for defining and instantiating classes in Max. The older API (using addmess() and setup() etc.) is limited to 32K. The newer API (using class_addmethod() etc, and currently documented in the pattrsdk.pdf in the SDK) is not limited at 32K.
      For large amounts of memory, however, you still will want to use pointers and allocate memory. I just wanted to clarify the 32k reference here.
      The newer API is preferred for Max 5. Among other things, it allows for the support of attributes.
      best, Tim