本文介绍了使用NSGlyph和内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在一个方法中,为 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:因为它是在栈上,你不必担心释放内存 - 因为从来没有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: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 ARCSo 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和内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 15:53