-
January 31st, 2019, 02:54 PM
#1
question
the following code has a memory leak, why?
Code:
class Base
{
public:
virtual void doSomething() = 0;
};
class Derived : public Base
{
public:
virtual void doSomething()
{
// Do the action
}
};
int main(int argc, char[] argv)
{
Derived* pDerived = new Derived;
Base* pBase = pDerived;
delete pBase;
return 0;
}
Last edited by Kmilano; February 1st, 2019 at 04:27 AM.
-
February 1st, 2019, 01:40 AM
#2
Re: question
Originally Posted by Kmilano
unfortunately the following attached code contains a memory leak.
How did you establish you have a memory leak?
Anyway, the Base class should have a virtual destructor otherwise it's undefined what happens.
https://www.quantstart.com/articles/...d-Memory-Leaks
Last edited by wolle; February 1st, 2019 at 01:50 AM.
-
February 1st, 2019, 03:18 AM
#3
Re: question
Originally Posted by Kmilano
the following code has a memory leak, why?
And where is your code snippet?
Victor Nijegorodov
-
February 21st, 2019, 01:49 PM
#4
Re: important
How did you find out the leak?
You can find a leak by yourself . Try to use some tools for finding leaks. For example, you can use Visual Leak Detector. This is easy to use, affordable tool. I use a more powerful tool - Deleaker. Or you can use Valgrind for Linux.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|