使用NSGlyph和内存分配 [英] Using NSGlyph and memory allocation
问题描述
在一个方法中,为 NSTextView visibleRect
频繁地跟踪换行符 strong> NSGlyph 以使用 NSLayoutManager getGlyphs:range:
。
应该/我可以找出多少内存这应该是,因为我有范围的引用(不影响布局),以及什么样的清理应该发生 - 运行 ARC ?
代码(在主队列上运行):
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];
for(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;
解决方案使用 NSGlyph glyphs [ 5000]
表示法,您正在分配堆栈上的内存。但是不是5000字形,它只需要保存 visibleRange.length + 1
glyphs:
< glyphArray
在输出中,来自 glyphRange $ b null终止。不会在结果中包含未显示的任何 NSNullGlyph
或
其他字形。传入的内存应该是大的
,至少 glyphRange.length + 1
元素。
因为它是在栈上,你不必担心释放内存 - 因为从来没有malloced内存;即使没有 ARC
,它会工作,如果你这样写:
NSLayoutManager * lm = ...
NSRange glyphRange = ...
NSGlyph glyphArray [glyphRange.length + 1] ;
NSUInteger numberOfGlyphs = [lm getGlyphs:glyphArray range:glyphRange];
//与字形做某事
in a method to track line breaks frequently, for a NSTextView visibleRect
, i am allocating memory for NSGlyph to use NSLayoutManager getGlyphs:range:
.
should/can i find out how much memory this should be since i have a reference for the range (without affecting layout), and also, what kind of cleanup should happen -- running with ARC ?
the code (which runs on a 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];
for (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 the NSGlyph glyphs[5000]
notation, you're allocating the memory on the stack. But instead of 5000 glyphs it only has to hold visibleRange.length + 1
glyphs:
glyphArray
On output, the displayable glyphs from glyphRange,
null-terminated. Does not include in the result any NSNullGlyph
or
other glyphs that are not shown. The memory passed in should be large
enough for at least glyphRange.length+1
elements.
And because it is on the stack, you don't have to worry about freeing the memory—because never malloced memory; it is freed automatically when leaving the function—even without ARC
So it should work, if you write it like this:
NSLayoutManager *lm = ...
NSRange glyphRange = ...
NSGlyph glyphArray[glyphRange.length + 1];
NSUInteger numberOfGlyphs = [lm getGlyphs:glyphArray range:glyphRange];
// do something with your glyphs
这篇关于使用NSGlyph和内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!