t_handle and sysmem_newhandle crash help needed

    May 21 2006 | 1:12 am
    Hi, I'm trying to read in a text file with sysfile_readtextfile, but I'm running into crashes using sysmem commands. Specifically, the following code (called thru defer_low) crashes:
    ------------------------------ void myrecorder_do_open(t_myrecorder* x, t_symbol *s, short ac, t_atom *av) { t_handle text_handle; long size;
    text_handle = sysmem_newhandle(20000); size = sysmem_handlesize(text_handle); } ------------------------------
    When I remove the sysmem_handlesize line it doesn't crash. I also printed out the text_handle pointer to make sure it wasn't NULL.
    Calls to sysmem_freehandle(&text_handle) crash Max as well.
    My plan was to sscanf thru the text_handle after filling it with the contents from a text file. Any suggestions or example code would be greatly appreciated. I'm developing using XCode, running Max 4.5.
    I've already been scouring the archives and only found this example of using t_handles: (which also crashes)
    short readtohandle(char *name, short volume, char ***h, long *sizep) { t_filehandle fh; long size; t_handle space;
    if (path_opensysfile(name, volume,&fh, PATH_READ_PERM)) { error("%s: can't open", name); return (1); } sysfile_geteof(fh,&size); if (!(space = sysmem_newhandle(size))) { ouchstring("Not enough memory to open %s",name); return (1); } if (sysfile_readtextfile(fh,space,0,TEXT_LB_NATIVE)) { readerror(name); sysmem_freehandle(space); sysfile_close(fh); return (1); } *h = space; *sizep = sysmem_handlesize(space); // could change in sysfile_readtextfile sysfile_close(fh); return (0); }

    • May 22 2006 | 7:21 pm
      Hi Scott,
      This sounds like it may be a problem with the Mach-O interface exposed in previous versions of MaxAPI.framework. I would suggest waiting a little while for a new version of MaxAPI.framework (sometime in the next month or two) before calling these functions which are crashing (sysmem_handlesize and sysmem_freehandle). In the meantime, you could either use Apple APIs for reading files, or use CodeWarrior. Sorry for the inconvenience.
    • May 22 2006 | 8:03 pm
      Thanks for the reply, I have since implemented my c-based text reader using the sysmem_newptr allocation function. I'm still not clear on the differences between t_handles and the memory allocated by sysmem_newptr. In any case, my function reads in a text file containing a list of notes, where each line contains things like vel, pitch, length. I'm posting my code since reading text files into max objects using c code (and Xcode) may be useful for others until new frameworks come out. WARNING: this is not "good" code, there is no error checking--a mis-formated input file would reap havoc. The code opens a dialog for the user to pick a file for reading, loads the entire contents into a newly allocated buffer, then parses that buffer line by line using sscanf and the 'n' char as a way to find the start and end of lines.
      void myrecorder_open(t_myrecorder* x, long arg) { t_atom myList[1]; SETLONG(myList,arg); defer_low(x,(method)myrecorder_do_open,0,1,myList); }
      void myrecorder_do_open(t_myrecorder* x, t_symbol *s, short ac, t_atom *av) { unsigned char p = av->a_w.w_long; // text file contains notes that will be stored into a phrase, pointed to by pp struct phrase_type * pp = &(x->phrases[p]); char filename[128]; short pathID; OSType dstType; t_filehandle *fh; char * text_handle; long size; int bytes_scanned; char teststring[128]; long cur_note; int i, start_of_line;
      // note attributes long onset, time_from_prev, time_till_next, hand; float vel, pitch;
      if (!open_dialog(filename, &pathID, &dstType,0L,0)) { path_opensysfile(filename, pathID, &fh, READ_PERM); sysfile_geteof(fh,&size); post("Storing file into phrase %d", p); text_handle = sysmem_newptr(size); if(text_handle) { // successful allocation if (!sysfile_read(fh, &size, text_handle)) { // phrase variables to adjust: pp->count = 0; pp->current_pos = 0; pp->total_length = 0; pp->tempo = 1.0; pp->current_time = 0.0; // first read in the top header line and ignore it i=0; while(text_handle[i]!='n') i++; i++; start_of_line = i;
      for(i;i // search for a n character so we know there's a line if (text_handle[i]=='n') { // found the end of a line sscanf(&text_handle[start_of_line],"%d %f %f %d %d %d",&onset,&pitch, &vel,&time_from_prev,&time_till_next,&hand); cur_note = pp->count; pp->notes[cur_note].onset_time = onset; pp->notes[cur_note].pitch = pitch; pp->notes[cur_note].vel = vel; pp->notes[cur_note].time_from_prev = time_from_prev; pp->notes[cur_note].time_till_next = time_till_next; pp->notes[cur_note].hand = hand; pp->count++; start_of_line = i+1; } }
      } else post ("error reading file"); } sysmem_freeptr(text_handle); else { post("memory allocation failed"); } sysfile_close(fh);
      } }