Forums > Dev

Segfault with function in macro – allowed in Max?

March 10, 2011 | 1:03 am

Hi there,

Working on bringing another very cool library into Max but have run into a big nasty brick wall. My C++ is even worse than my C, so please take this with a grain of salt.

One of the class files (.cc) I’m working with has this:

void homography_estimator::reset_correspondences(int maximum_number_of_correspondences)
{
  manage_buffer(u_v_up_vp,            4 * maximum_number_of_correspondences, float);
  manage_buffer(normalized_u_v_up_vp, 4 * maximum_number_of_correspondences, float);
  manage_buffer(scores,                   maximum_number_of_correspondences, float); //< ---crashes here
  manage_buffer(sorted_ids,               maximum_number_of_correspondences, int); //<--- and sometimes here.

  number_of_correspondences = 0;
}

Manage_buffer does something I've never seen before. It defines the functions in the macros. I don't think this is a coincidence that it crashes here.

// Macros for the functions defined above:
#define manage_buffer(buffer, required_size, type)			
  if ((buffer) != 0 && ( *(int*)(((char*)(buffer)) - sizeof(void*) - sizeof(int)) < (required_size)) ) { 
    aligned_and_managed_delete(buffer);					
    (buffer) = 0;							
  }									
  if ((buffer) == 0) {							
    int size = (required_size) * sizeof(type);				
    buffer = (type*)(aligned_and_managed_new16b(size));			
    if (MANAGED_MEMORY_DEBUGGING)					
      cout << endl							
	   << "[MANAGED_MEMORY_DEBUGGING: allocating "			
	   << (required_size) << " x " << sizeof(type)			
	   << " bytes at t" << (void*)(buffer) << "]" << endl;		
    if ((buffer) == 0)							
      cerr << ">! [buffer_management] MACRO manage_buffer: can't alloc " < < size << " bytes." << endl; 
    else								
      *(int*)(((char*)buffer) - sizeof(void*) - sizeof(int)) = required_size; 
  }

Is it something within the function I'm not understanding that's causing the problem, or is declaring the function in a macro not compatible with the way Max/MSP works?

Any help is appreciated.

Thanks.


March 10, 2011 | 5:55 am

Hi,

#define directives are quite normal in C, see for instance

http://en.wikipedia.org/wiki/C_preprocessor#Macro_definition_and_expansion

You’re getting the trouble for what is defined in the function manage_buffer, but to guess this, one needs more information (for instance, the error you get and what the other invoked functions do).

Bests,
Ádám


March 10, 2011 | 4:28 pm

Hi Siska,

Thanks for your response! Your post reminded me to try the MANAGED_MEMORY_DEBUGGING macro and see if that’s what’s causing the segfault (as you suggested) by tracking down which functions are the problem.

Though I have never debugged a function in a Macro before… I can tell this is going to be painful. Any tips?

Thanks so very much for your response though! This is helpful and at least shows me I’m not wasting my time!

Will post back as things progress.


Viewing 3 posts - 1 through 3 (of 3 total)