gpt4 book ai didi

c - 我需要释放 ptsname() 的结果吗?

转载 作者:行者123 更新时间:2023-11-30 14:58:27 25 4
gpt4 key购买 nike

当我调用 ptsname() 时,我会得到一个 char* 返回。

联机帮助页没有指定其链接、所有权或生命周期,但 valgrind 显示它导致了泄漏(使用 --leak-check=full)。

==46958== 128 bytes in 1 blocks are definitely lost in loss record 41 of 65
==46958== at 0x10010FEBB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==46958== by 0x1003F9682: ptsname (in /usr/lib/system/libsystem_c.dylib)
==46958== by 0x10001BA5F: startJob(childproc*) (unix-base.cc:211)
==46958== by 0x100019CAB: stepChild(childproc*, std::__1::function<bin::Job* (bin::Job*)>) (unix-base.cc:281)
==46958== by 0x100018F2C: bin::runJobs(std::__1::function<bin::Job* (bin::Job*)>, int) (unix-base.cc:350)
==46958== by 0x1000027FC: pmain() (bin.cc:65)
==46958== by 0x100003787: main (bin.cc:90)

然而,在free()-ing结果时,我得到了通常的未malloc'd错误:

bin(46690,0x7fff76531000) malloc: *** error for object 0x7fb35af00f90: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
./bootstrap.sh: line 41: 46690 Abort trap: 6 bin/bin

这是否只是该平台上 ptsname() 内部实现的泄漏,还是我应该(以某种方式)释放结果?

最佳答案

该库分配该内存,文档称该库可能会重用该内存,因此您不应该释放它。

On success, ptsname() returns a pointer to a string in static storage which will be overwritten by subsequent calls. This pointer must not be freed. On failure, a NULL pointer is returned.
The man page

您应该忽略此 valgrind 警告。您可以告诉 valgrind 为您忽略它。

--ignore-fn=<name>
Any direct heap allocation (i.e. a call to malloc, new, etc, or a call to a function named by an --alloc-fn option) that occurs in a function specified by this option will be ignored. This is mostly useful for testing purposes. This option can be specified multiple times on the command line, to name multiple functions.
The man page

关于c - 我需要释放 ptsname() 的结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43334567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com