CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: important

  1. #1
    Join Date
    Mar 2018
    Posts
    40

    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.

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: question

    Quote Originally Posted by Kmilano View Post
    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.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: question

    Quote Originally Posted by Kmilano View Post
    the following code has a memory leak, why?
    And where is your code snippet?
    Victor Nijegorodov

  4. #4
    Join Date
    Dec 2018
    Posts
    18

    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
  •  





Click Here to Expand Forum to Full Width

Featured