# Char \* ExpandPathName(const char\* path) memory leak

**URL:** <https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377>\
**Category:** ROOT\
**Created:** [July 15, 2013, 1:51pm UTC](https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377 "2013-07-15T13:51:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [July 15, 2013, 1:51pm UTC](https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377/1 "2013-07-15T13:51:48Z")

</div>

Dear experts,

with valgrind I am finding many

```auto
==2209== 125 bytes in 1 blocks are definitely lost in loss record 36,326 of 47,028
==2209== at 0x4A065BA: operator new[](unsigned long) (vg_replace_malloc.c:264)
==2209== by 0x7AF1315: StrDup(char const*) (in /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/5.34.07-x86_64-slc5-gcc4.3/lib/libCore.so)
==2209== by 0x7B8B1C4: TUnixSystem::ExpandPathName(char const*) (in /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/5.34.07-x86_64-slc5-gcc4.3/lib/libCore.so)
```

I was not able to find the original StrDup, but for example in core/base/inc/TString.h there is an extern declaration with a comment:

```auto
extern char *StrDup(const char *str); // duplicate str, free with delete []
```

should I free the returned char\* from ExpandPathName? Please document it.

By the way, what is StrDup? I know strdup:

```auto
The strdup() function shall return a pointer to a new string, which is a duplicate of the string pointed to by s1. The returned pointer can be passed to free(). A null pointer is returned if the new string cannot be created.
```

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [July 15, 2013, 2:09pm UTC](https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377/2 "2013-07-15T14:09:39Z")

</div>

finally I have found the function in core/base/src/TString.cxx

```auto
char *StrDup(const char *str)
{
   // Duplicate the string str. The returned string has to be deleted by
   // the user.

   if (!str) return 0;

   char *s = new char[strlen(str)+1];
   if (s) strcpy(s, str);

   return s;
}
```

1. you should move this comment in all the function returning a char\* created with this function
2. why don’t use strdup?

---

<div class="post-metadata">

**Author:** ![tpochep](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/f05b48/32.png) [@tpochep](https://root-forum.cern.ch/u/tpochep)\
**Post date:** [July 15, 2013, 7:05pm UTC](https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377/3 "2013-07-15T19:05:27Z")

</div>

[quote]  
2. why don’t use strdup?[/quote]

May be, because strdup is not the part of the C/C++ standard libraries?

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [July 15, 2013, 7:42pm UTC](https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377/4 "2013-07-15T19:42:19Z")

</div>

[quote=“tpochep”][quote]  
2. why don’t use strdup?[/quote]

May be, because strdup is not the part of the C/C++ standard libraries?[/quote]

yes, you are right, it is posix. Probably it is not standard because of the hidden malloc.

---

<div class="post-metadata">

**Author:** ![tc3t](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@tc3t](https://root-forum.cern.ch/u/tc3t)\
**Post date:** [July 15, 2013, 7:46pm UTC](https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377/5 "2013-07-15T19:46:45Z")

</div>

> [@wiso](#):
>
> should I free the returned char\* from ExpandPathName? Please document it.

Documentation of TUnixSystem::ExpandPathName seems to have lost that somewhere (at least in the online documentation), but [TSystem::ExpandPathName](http://root.cern.ch/root/html534/TSystem.html#TSystem:ExpandPathName@1) says:

> [@ROOT documentation](#):
>
> The user must delete returned string.

It does not, however, specify whether user should use delete or delete, which might make it easy to mix those possibly leading, [as noted by some](http://www.parashift.com/c++-faq-lite/delete-array-built-ins.html), to disastrous bugs.

> [@wiso](#):
>
> 2. why don’t use strdup?

In addition to the reason pointed out by tpochep, strdup returns pointer that should be deallocated with free() instead of delete. C++ library such as ROOT could perhaps have tried to avoid providing interfaces that require the use free(), although the delete-issue with new/delete style might be more of a problem.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 29, 2013, 4:31pm UTC](https://root-forum.cern.ch/t/char-expandpathname-const-char-path-memory-leak/16377/6 "2013-07-29T16:31:21Z")

</div>

Hi,

The documentation was updated. You may be interested in using:`TSystem::ExpandPathName(TString &path);`which avoid the need to explicit deallocation.

Cheers,  
Philippe.
