grrrwaaa/maxcpp

Bug in setupIO

Closed this issue · 1 comments

Hello Graham,

I tried out your code on Mac OS X 10.8.4 , running Max 6.1.2. in 32 bit mode and 64 bit mode.
When I run Max in 64bit mode, your code crashes Max.
It appears as if it would write to memory locations where it should not.

I think I found the questionable point in your code. It's in setupIO, when you get system memory to store the inlet proxies.

void setupIO(unsigned int numinlets = 1, unsigned int numoutlets = 1) {

[...]

m_inletproxies = (void **)sysmem_newptr(sizeof(void *) * numproxies);

[...]

for (unsigned int i=1; i<=numproxies; i++)
   m_inletproxies[i] = proxy_new(this, i, &this->m_whichinlet);

When i == numproxies, you write to memory that you did not allocate.

When I change the code to:

for (unsigned int i=0; i<numproxies; i++)
   m_inletproxies[i] = proxy_new(this, i+1, &this->m_whichinlet);

the external works fine.

I don't know exactly why this problem doesn't show up in 32 bit mode.

Then I wonder if you should release the memory you allocated in

static void maxcpp_destroy(t_object * x) 

Maybe like this:

unsigned long numinletproxies;
numinletproxies = sysmem_ptrsize(t->m_inletproxies)/sizeof(void*);

for (unsigned int i=0; i < numinletproxies; i++)
   object_free(t->m_inletproxies[i]);

sysmem_freeptr(t->m_inletproxies);
sysmem_freeptr(t->m_outlets);

This works for my system in 64bit and 32bit mode.

Kind regards,

Jan

I seem to have missed this issue notification, so I apologize for the delay in responding! Both fixes look great and have been committed.