cocoa - Using NSGlyph and memory allocation -


in method track line breaks frequently, nstextview visiblerect, allocating memory nsglyph use nslayoutmanager getglyphs:range:.

should/can find out how memory should since have reference range (without affecting layout), , also, kind of cleanup should happen -- running arc ?

the code (which runs on main queue) :

    nslayoutmanager *lm = [self.textview layoutmanager];     nstextcontainer *tc = [self.textview textcontainer];     nsrect vrect = [self.textview visiblerect];     nsrange visiblerange = [lm glyphrangeforboundingrectwithoutadditionallayout:vrect intextcontainer:tc];     nsuinteger vrangeloc = visiblerange.location;     nsuinteger numberoflines;     nsuinteger index;     nsglyph glypharray[5000]; // <--- memory assigned here     nsuinteger numberofglyphs = [lm getglyphs:glypharray range:visiblerange];     nsrange linerange;     nsmutableindexset *idxset = [nsmutableindexset indexset];     (numberoflines = 0, index = 0; index < numberofglyphs; numberoflines++) {         (void)[lm linefragmentrectforglyphatindex:index effectiverange:&linerange withoutadditionallayout:yes];         [idxset addindex:linerange.location + vrangeloc];         index = nsmaxrange(linerange);     }     self.currentlinesindexset = idxset; 

with nsglyph glyphs[5000] notation, you're allocating memory on stack. instead of 5000 glyphs has hold visiblerange.length + 1 glyphs:

glypharray

on output, displayable glyphs glyphrange, null-terminated. not include in result nsnullglyph or other glyphs not shown. memory passed in should large enough @ least glyphrange.length+1 elements.

and because on stack, don't have worry freeing memory—because never malloced memory; freed automatically when leaving function—even without arc

so should work, if write this:

nslayoutmanager *lm = ... nsrange glyphrange = ... nsglyph glypharray[glyphrange.length + 1]; nsuinteger numberofglyphs = [lm getglyphs:glypharray range:glyphrange]; // glyphs 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -