Non-alloc/copy Methods

本文探讨了Objective-C中非alloc或copy方法的使用,解释了为什么从这些方法中获取的对象不需要显式释放。通过一个NSMutableString的例子说明了内存管理的具体实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Non-alloc/copy Methods 


+ (NSMutableString *)string

{ 

return [[[self alloc] init] autorelease]; 

 

 

Let’s revisit why you don’t have to release objects from non-alloc or 

copy methods 


NSMutableString *theString = [NSMutableString string]; 


// You don’t have to release theString because you didn’t 

// alloc or copy it

ld.lld: error: undefined symbol: kasan_init_hw_tags >>> referenced by smp.c:471 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/arch/arm64/kernel/smp.c:471) >>> vmlinux.o:(smp_prepare_boot_cpu) ld.lld: error: undefined symbol: __kasan_cache_create_kmalloc >>> referenced by kasan.h:143 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:143) >>> vmlinux.o:(create_kmalloc_cache) ld.lld: error: undefined symbol: kasan_flag_enabled >>> referenced by irqbypass.c >>> vmlinux.o:(__jump_table+0xE58) >>> referenced by irqbypass.c >>> vmlinux.o:(__jump_table+0xE88) >>> referenced by irqbypass.c >>> vmlinux.o:(__jump_table+0x5FD8) >>> referenced 894 more times ld.lld: error: undefined symbol: kasan_init_hw_tags_cpu >>> referenced by cpufeature.c:1877 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/arch/arm64/kernel/cpufeature.c:1877) >>> vmlinux.o:(cpu_enable_mte) ld.lld: error: undefined symbol: kasan_report_async >>> referenced by mte.c:187 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/arch/arm64/kernel/mte.c:187) >>> vmlinux.o:(mte_check_tfsr_el1) >>> referenced by mte.c:187 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/arch/arm64/kernel/mte.c:187) >>> vmlinux.o:(mte_thread_switch) >>> referenced by mte.c:187 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/arch/arm64/kernel/mte.c:187) >>> vmlinux.o:(mte_suspend_enter) ld.lld: error: undefined symbol: kasan_report >>> referenced by fault.c:334 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/arch/arm64/mm/fault.c:334) >>> vmlinux.o:(__do_kernel_fault) ld.lld: error: undefined symbol: __kasan_unpoison_range >>> referenced by kasan.h:111 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:111) >>> vmlinux.o:(dup_task_struct) >>> referenced by kasan.h:111 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:111) >>> vmlinux.o:(mempool_exit) >>> referenced by kasan.h:111 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:111) >>> vmlinux.o:(remove_element) >>> referenced 3 more times ld.lld: error: undefined symbol: __kasan_unpoison_vmalloc >>> referenced by kasan.h:416 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:416) >>> vmlinux.o:(scs_alloc) >>> referenced by kasan.h:416 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:416) >>> vmlinux.o:(scs_free) >>> referenced by kasan.h:416 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:416) >>> vmlinux.o:(vm_map_ram) >>> referenced 2 more times ld.lld: error: undefined symbol: __kasan_poison_vmalloc >>> referenced by kasan.h:425 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:425) >>> vmlinux.o:(scs_alloc) >>> referenced by kasan.h:425 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:425) >>> vmlinux.o:(vm_unmap_ram) >>> referenced by kasan.h:425 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:425) >>> vmlinux.o:(__vunmap) ld.lld: error: undefined symbol: __kasan_unpoison_pages >>> referenced by kasan.h:127 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:127) >>> vmlinux.o:(mempool_exit) >>> referenced by kasan.h:127 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:127) >>> vmlinux.o:(remove_element) >>> referenced by kasan.h:127 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:127) >>> vmlinux.o:(mempool_resize) >>> referenced 4 more times ld.lld: error: undefined symbol: __kasan_poison_pages >>> referenced by kasan.h:119 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:119) >>> vmlinux.o:(mempool_init_node) >>> referenced by kasan.h:119 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:119) >>> vmlinux.o:(mempool_resize) >>> referenced by kasan.h:119 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:119) >>> vmlinux.o:(mempool_free) >>> referenced 2 more times ld.lld: error: undefined symbol: __kasan_slab_free_mempool >>> referenced by kasan.h:208 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:208) >>> vmlinux.o:(mempool_init_node) >>> referenced by kasan.h:208 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:208) >>> vmlinux.o:(mempool_resize) >>> referenced by kasan.h:208 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:208) >>> vmlinux.o:(mempool_free) ld.lld: error: undefined symbol: __kasan_never_merge >>> referenced by kasan.h:103 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:103) >>> vmlinux.o:(slab_unmergeable) >>> referenced by kasan.h:103 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:103) >>> vmlinux.o:(find_mergeable) >>> referenced by kasan.h:103 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:103) >>> vmlinux.o:(find_mergeable) ld.lld: error: undefined symbol: __kasan_kmalloc_large >>> referenced by kasan.h:237 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:237) >>> vmlinux.o:(kmalloc_order) ld.lld: error: undefined symbol: __kasan_check_byte >>> referenced by kasan.h:259 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:259) >>> vmlinux.o:(krealloc) >>> referenced by kasan.h:259 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:259) >>> vmlinux.o:(kfree_sensitive) >>> referenced by kasan.h:259 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:259) >>> vmlinux.o:(ksize) ld.lld: error: undefined symbol: __kasan_krealloc >>> referenced by kasan.h:247 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:247) >>> vmlinux.o:(krealloc) ld.lld: error: undefined symbol: kasan_free_pages >>> referenced by page_alloc.c:1361 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/mm/page_alloc.c:1361) >>> vmlinux.o:(__free_pages_ok) >>> referenced by page_alloc.c:1361 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/mm/page_alloc.c:1361) >>> vmlinux.o:(free_unref_page_prepare) ld.lld: error: undefined symbol: kasan_alloc_pages >>> referenced by page_alloc.c:2410 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/mm/page_alloc.c:2410) >>> vmlinux.o:(post_alloc_hook) >>> referenced by page_alloc.c:2410 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/mm/page_alloc.c:2410) >>> vmlinux.o:(prep_new_page) ld.lld: error: undefined symbol: __kasan_metadata_size >>> referenced by kasan.h:150 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:150) >>> vmlinux.o:(print_trailer) >>> referenced by kasan.h:150 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:150) >>> vmlinux.o:(check_object) ld.lld: error: undefined symbol: __kasan_kmalloc >>> referenced by kasan.h:227 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:227) >>> vmlinux.o:(kmem_cache_alloc_trace) >>> referenced by kasan.h:227 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:227) >>> vmlinux.o:(__kmalloc) >>> referenced by kasan.h:227 (/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/include/linux/kasan.h:227) >>> vmlinux.o:(__kmalloc_track_caller) ld.lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors) make[1]: *** [/home/lhl/ga-4.1/repo_code/out/oriole/kernel/src_tmp/linux-5.15/Makefile:1277: vmlinux] Error 1 make[1]: Leaving directory '/home/lhl/ga-4.1/repo_code/out/oriole/kernel/OBJ/linux-5.15' make: *** [Makefile:237: __sub-make] Error 2
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值