I am using a GCC compiler on Ubuntu 14.04.
I have a small communication app (not written by me) and I intend to embed it into my own app.
The small comm. app code looks like this:
UartComm * commInterface;
...
void commInit()
{
commInterface = new UartComm;
}
// other functions here that use "commInterface"
// ...
int main()
{
commInit();
...
if(smthing_1)
return (-1);
if(smthing_2)
return (-2);
...
if(smthing_n)
return (-n);
//
return (0);
}
--
As seen above there is no delete commInterface;
in the original code so, if I embed the above code into my app, rename main()
to commFunction()
and call it many times, I will have a lot of memory that is not de-allocated.
The above code is a simplified version. In fact, the code has many exit points/returns. It also has some functions that throw exceptions (I'm not 100% sure they're all catched and handled properly).
So, I guess adding delete commInterface;
before all returns
will not be enough in this case...
Hence, my question: Is there a way to properly delete/release commInterface
in order to embed and use the above module without worrying about memory leaks? Smart pointer maybe or some other idea...?
Two remarks:
1) I've enabled C++11;
2) I' not using (and don't wanna use) boost.
Thanks in advance for your time and patience.