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

Do I need to free the result from ptsname()?

当我打电话给 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' 错误:

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