I am getting a memory leak on the memory dump using gdb in c++. I am not able to figure out the reason for the memory leak, I have shared the code which I am using. And when does the memory of the string freed when the string is initialized using string constructor?
code-
#include<bits/stdc++.h>
using namespace std;
string getpass(string mypass)
{
string my="";
for(int i=0;i<mypass.length();i++)
{
my+= mypass[i]+1;
}
return my;
}
string all()
{
string mypass = "shivam";
string rdpCommand;
rdpCommand = string("/opt/xfreerdp ") + string(" /ppppp:'")+getpass(mypass)+ string("' ");
rdpCommand.clear();
rdpCommand.shrink_to_fit();
string command ="";
return command;
}
int main(){
string t=all();
sleep(100); //using sleep so the execution doesn't terminate and I can take memory dump
}
The script I am using to take memory dump-
#!/bin/bash
grep rw-p /proc/$1/maps \
| sed -n 's/^\([0-9a-f]*\)-\([0-9a-f]*\) .*$/\1 \2/p' \
| while read start stop; do \
gdb --batch --pid $1 -ex \
"dump memory $1-$start-$stop.dump 0x$start 0x$stop"; \
done
Run above script by sudo ./script.sh [pid_of_process]
Screenshot showing memory dump-
Edit: I have tried the OpenSSL way of securing string but it didn't work and tried writing allocator and deallocator but it is giving error in linux. Both answers are in this link. how does one securely clear std::string?