[LLVMdev] getElapsedWallTime unnecessary heap allocation and memory leak

Bryan Keiren bryan.keiren at guerrilla-games.com
Wed Mar 19 06:17:07 PDT 2014


In the file \lib\Support\Process.cpp on line 60, it seems as though an unnecessary heap allocation and memory leak occurs.

This is the offending code:

static TimeValue getElapsedWallTime() {
  static TimeValue &StartTime = *new TimeValue(TimeValue::now());
  return TimeValue::now() - StartTime;
}

The issue is that the StartTime variable's value is allocated on the heap, after which a *reference* to it is stored (not the pointer itself). This means that the allocated memory is actually never properly de-allocated.
Granted, the memory leak is small but if anyone attempts to write memory leak-free software while including LLVM code, this will be an issue (as it currently is for our company).


Is there anyone who knows why exactly this was implemented the way it currently is implemented? It seems rather unnecessary to be implemented with this complexity when a simpler implementation would work as well:

static TimeValue getElapsedWallTime() {
  static TimeValue TimeValue(TimeValue::now());
  return TimeValue::now() - StartTime;
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140319/4b247c4c/attachment.html>


More information about the llvm-dev mailing list