Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7932 Discussions

Memory leak: Temporary std::set<std::string> is not deleted.

Bastian_B_
New Contributor I
1,385 Views

The following program:

 

#include <set>
#include <iostream>

std::set<std::string> Foo() {

  std::set<std::string> ret;
  ret.insert("test");
  return ret;
}

int main(int argc, char** argv) {

  while (true) {
    for(auto& s : Foo()) {
      // do nothing.
    }
  }

  return 0;
}

Leaks memory and causes my system to swap after a few seconds. This is on Linux x86_64, ICC Version 17.0.1, GCC Version 6.3.0. I compile like this:

icpc -o test test.C

Compiling the same program with g++ does not show this leak. Any ideas? Is there a bug in the compiler?

0 Kudos
1 Solution
Judith_W_Intel
Employee
1,385 Views

 

This is a known defect with temporaries and range based for loops in 17.0 update 1.

It is fixed in the soon to be release update 2.

Sorry for the inconvenience.

See this post for a detailed discussion of the bug:

https://software.intel.com/en-us/forums/intel-c-compiler/topic/705086

 

Judy

View solution in original post

0 Kudos
9 Replies
Bastian_B_
New Contributor I
1,385 Views

The bug goes away in this version:

#include <set>
#include <iostream>

std::set<std::string> Foo() {

  std::set<std::string> ret;
  ret.insert("test");
  return ret;
}

int main(int argc, char** argv) {

  while (true) {
    std::set<std::string> foo = Foo();
    for(auto& s : foo) {
      // do nothing.
    }
  }

  return 0;
}

i.e. when first storing the result of Foo() in a temporary.

0 Kudos
Nikolas_Z_
Beginner
1,385 Views

Bug confirmed!

I wish someone from Intel could look into this and check.

The same happens when using a std::vector<std::string> instead of std::set<std::string>. Same sympton: when bining to a local variable before the range-based for loop, there is no issue, otherwise it leaks.

0 Kudos
Judith_W_Intel
Employee
1,386 Views

 

This is a known defect with temporaries and range based for loops in 17.0 update 1.

It is fixed in the soon to be release update 2.

Sorry for the inconvenience.

See this post for a detailed discussion of the bug:

https://software.intel.com/en-us/forums/intel-c-compiler/topic/705086

 

Judy

0 Kudos
Nikolas_Z_
Beginner
1,385 Views

Thanks a lot Judy!

0 Kudos
Bastian_B_
New Contributor I
1,385 Views

Thank you Judy, we have applied a workaround along the lines of the second code snippet I posted and are looking forward to 17.0. Update 2.

0 Kudos
SergeyKostrov
Valued Contributor II
1,385 Views
>>... >>I compile like this: >> >>icpc -o test test.C There are compilation errors if option -std=c++11 is not used. >>.. .>>Leaks memory and causes my system to swap after a few seconds. I couldn't reproduce the problem. I'll provide more technical details on a Linux system and version of Intel C++ compiler.
0 Kudos
SergeyKostrov
Valued Contributor II
1,385 Views
>>I couldn't reproduce the problem... Correction. Memory leaks confirmed for version 17.0.1 Update 1: ... C++ Language Standard version: 199711L Intel C++ compiler Build Date: 20161005 ...
0 Kudos
SergeyKostrov
Valued Contributor II
1,385 Views
Older versions of Intel C++ compilers, for example 16.0.3, passed the test: [guest@... WorkTest]$ icpc test01.c -o test01.out C++ Language Standard version: 199711L Intel C++ compiler Build Date: 20160415 test01.c(131): error #303: explicit type is missing ("int" assumed) for( auto& s : Foo() ) ^ test01.c(131): error: reference variable "s" requires an initializer for( auto& s : Foo() ) ^ test01.c(131): error: expected a ";" for( auto& s : Foo() ) ^ test01.c(131): error: expected an expression for( auto& s : Foo() ) ^ test01.c(131): error: expected a ";" for( auto& s : Foo() ) ^ compilation aborted for test01.c (code 2) [guest@... WorkTest]$ [guest@... WorkTest]$ icpc -std=c++11 test01.c -o test01.out C++ Language Standard version: 201103L Intel C++ compiler Build Date: 20160415 [guest@... WorkTest]$ [guest@... WorkTest]$ date Tue Feb 21 11:19:49 PST 2017 [guest@... WorkTest]$ [[guest@... WorkTest]$ ./test01.out ^C [guest@... WorkTest]$ [guest@... WorkTest]$ date Tue Feb 21 11:21:01 PST 2017 No any memory leaks.
0 Kudos
Bastian_B_
New Contributor I
1,385 Views

Thanks for the additional tests Sergey, I believe with 17.0 Update 1 and GCC 6.3.0 -std=c++11 is default so I didn't add it in the flags.

0 Kudos
Reply