Memory leak somewhere?

Discussion in 'Programmer's Corner' started by Peytonator, Nov 12, 2011.

  1. Peytonator

    Thread Starter Active Member

    Jun 30, 2008
    105
    3
    Hi,

    I've attached a portion of code where there is apparently a memory leak. Where is the leak? I can't find it.

    Thanks :)
     
  2. stahta01

    Member

    Jun 9, 2011
    133
    21
    The leak is likely in the code you did not post; but, I am a C programmer.
    So, I might have missed a mistake in the posted code.

    NOTE: If you posted the code in Code tags you are more likely to get a response.
    Use the "Go Advanced" button to see the "#" code tag button.

    Tim S.
     
  3. Peytonator

    Thread Starter Active Member

    Jun 30, 2008
    105
    3
    The problem is definitely in the code I've attached. Unfortunately it comes directly out of a past exam paper (pdf) so I can't put it in code tags...

    Thanks for the response nontheless :)
     
  4. stahta01

    Member

    Jun 9, 2011
    133
    21
    If it is NOT worth you time to rewrite the code or you are cheating by posting this question then it not worth my time to help.

    Tim S.
     
  5. Peytonator

    Thread Starter Active Member

    Jun 30, 2008
    105
    3
    What's the point of rewriting the code if it's just as clear in a jpg?

    Cheating? Notice the date on the exam: 2007
     
  6. TheFox

    Active Member

    Apr 29, 2009
    66
    5
    Still you want us to work for you, to help you...
     
  7. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    I think I've identified the problem but can you post it in code tags to make it more readable please ?
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    See post #3. I can't figure that one out. I guess he is compiling the image of the exam. :)
     
    strantor likes this.
  9. samin

    Member

    Oct 14, 2011
    32
    6
    Me neither, I can't see any mistake in the posted code .. I think Also and as mentioned in the posted PDF file it would be necessary to rewrite the code as it must be will commented and easily understandable.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Good point. I think what we really need to see is the code that is being compiled not the code in the PDF.


    So where is the OP?
     
  11. TheFox

    Active Member

    Apr 29, 2009
    66
    5
    I think he got mad that people wouldn't rewrite it for him. I do hope I'm wrong however.
     
  12. ToBeFrank

    New Member

    Nov 18, 2010
    13
    0
    You don't have to compile this code to see that it is definitely broken.
     
  13. Peytonator

    Thread Starter Active Member

    Jun 30, 2008
    105
    3
    Yup, there's no need to compile it. The problem which I've found is that actually the destructor is not declared virtual, so both the base and derived classes call the base class destructor. Thus the _dept string still remains intact.
     
  14. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Humm that one is a new one on me. I have be away from C++ for a while but I never remember having a need to declare a destructor virtual.

    The issue is that you are saving an Manager object in an Employee point so when you delete it the Employee destructor gets called no the manager as it should be,

    Perhaps declaring the desctructor variable fixes this but you need to be certain the manager destructor is being called or you will still have a memory leak.
     
  15. ToBeFrank

    New Member

    Nov 18, 2010
    13
    0
    Declaring the destructor virtual does that, and it is the correct fix for this code.
     
  16. MCrowe

    Member

    May 29, 2011
    69
    0
    I dont understand why every question someone asks on here, the first thing someone does is jump on them telling them there lazy, or cheating and they're not answering there question...

    Sure some coments are helpful, like telling a new person they are meant to use code tags, but after that, if you dont want to answer the question, then don't. It seems like the original question was a question asked in a previous exam, probably on paper not on a computer, so it would be resonable to expect the answer could be obtained without compiling the code, as it was...
     
  17. Georacer

    Moderator

    Nov 25, 2009
    5,142
    1,266
    When you do programming training, the least you can do when you work a problem is to type it, compile it and then check for compilation errors. Anything less isn't really considered effort.

    Past paper or not, any question worth answering should at least be backed up with some effort from the OP.
     
  18. ToBeFrank

    New Member

    Nov 18, 2010
    13
    0
    The problem in this case is compiling and running this example does not gain anything. It will appear to work just fine. One could argue you could then put it through valgrind or what not, but I wouldn't expect a student to know about tools like that. I have to agree with the OP that putting it in a compilable form is not necessary. In my opinion, for this particular example, anyone that can't see the problem and requires it to be compiled is probably not qualified to answer the question.
     
  19. stahta01

    Member

    Jun 9, 2011
    133
    21
    There are too many people posting question like this using smart phones during tests. That is why I asked them to post the text of the question. This would permit a Google search to confirm the question was posted by the school.

    Note: I am a C programer learning C++ and one of my two guesses was the destructor was missing/wrong; So, I was almost right.

    Edit: I have taken classes that reuse test questions from passed years.

    Tim S.
     
    Last edited: Nov 24, 2011
  20. ToBeFrank

    New Member

    Nov 18, 2010
    13
    0
    Sorry, didn't mean to single anyone out. It's really a C++ question so I wouldn't expect someone who doesn't use C++ regularly to recognize the problem right away.
     
Loading...