2017-04-10 81 views
0

当我打电话给ptsname()时,我得到一个char*回来。我需要从ptsname()中释放结果吗?

联机帮助页未指定其链接,所有权或生命周期,但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()在这个平台上泄露,还是应该我(以某种方式)释放结果?

+1

'ptsnmame'是非新生的,因为它返回一个静态存储,可以考虑使用重入版本'ptsname_r'。 – fluter

回答

1

库分配该内存,并且文档说该库可能会重用该内存,所以您应该而不是释放它。

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