Reputation: 15103
When I call ptsname()
I get a char*
back.
The manpage doesn't specify its linkage, ownership or lifetime, but valgrind is showing that it's causing a leak (with --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)
However, upon free()
-ing the result I get the usual un-malloc
'd error:
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
Is this just a leak in the internal implementation of ptsname()
on this platform, or should I be (somehow) freeing the result?
Upvotes: 0
Views: 162
Reputation: 70412
The library allocates that memory, and the documentation says the library may reuse that memory, so you should not free it.
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, aNULL
pointer is returned.
The man page
You should ignore this valgrind warning. You can tell valgrind to ignore it for you.
--ignore-fn=<name>
Any direct heap allocation (i.e. a call tomalloc
,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
Upvotes: 1