Thread: memory leak

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    memory leak

    I am having a problem with what I think is a memory leak in my program. I am creating a vector of chars, and then resizing it. I am stepping through the code using the debugger, so I can see that I am resizing it to 250. The code I am using to do this is as follows:

    Code:
    vector<char> mask;
    mask.resize(prfLength + 1);
    But when I check the size afterwards it turns out to be over 4 billion (4GB). It seems to have allocated all of the remaining virtual memory on the machine.

    I have tried using valgrind to isolate the problem. But the errors I am getting are not really telling me anything. Can anyone recommend any other tools that I can use to try to find what the problem is?
    Thanks
    Mark

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Are you sure about that? That wouldn't make anysense that resizing a vector of chars to 250 would make a memory leak of that size
    Woop?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    Well, I am thinking that maybe some memory problem earlier on is causing this problem. I dont know why else the vector would be resized to over 4 billion. I can see that the number I am actually trying to resize it to is only 250. I have tried using a string and assigning caharacters to it using the same length(prfLength + 1). This ends up with the correct size.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Has prfLength been initialized?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    how is prfLength declared and initialized?
    And what's the compiler you are using?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    The code is very long (about 25000 lines) so I cant show it all here
    The variable prfLength is passed into a function with the following prototype:

    Code:
    void calcGapPenaltyMask(int prfLength, vector<char>* mask, vector<char>* gapMask);
    The function is then called as follows:

    Code:
    int _length = alignmentObj.getSeqLength(1);
    calcGapPenaltyMask(_length, getSecStructMask(), getGapMask());
    I can see that the _length variable has the value that I expect using the debugger. I have run valgrind on it, and there is definitely some problems. But it seems to be pointing to invalid read and writes in the libstdc++ library. But im sure this isnt the case.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    I am using g++ 3.4.2

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, mask is actually a pointer to a vector<char>, not a vector.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    No. Sorry, I have just taken bits from the code. In my program the vector that I am resizing is called structMask. I was just trying to illustrate a point. So it would be

    Code:
    vector<char> structMask;
    structMask.resize(prfLength + 1);
    It is this vector that gets the size set to 4 billion, even though prfLength is 250.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you tried writing the smallest and simplest program that demonstrates this problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    In the very least, we need to know what is happening inside calcGapPenaltyMask... assuming that's where the resize is happening.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But the errors I am getting are not really telling me anything
    Since you didn't post them, they're not telling us anything either

    As laserlight says, post some code we can run which shows the problem.
    Reposting "this line doesn't work" doesn't help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    As a side note...

    Do you need to use a vector<char>? Strings are nearly identical to vectors and support most of the operations vectors do. Typedefs (including iterators), constructors and assignment operations, push_front, size operations and even the memory allocation operations resize and reserve.

    They also provide specific operations, namely specialized ones to deal with the close integration between C-style strings and C++ strings (the reason I think perhaps you choose a vector)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> when I check the size afterwards it turns out to be over 4 billion
    How are you checking the size? If you are using the debugger to check a member variable of the vector, you might be looking in the wrong place.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void calcGapPenaltyMask(int prfLength, vector<char>* mask, vector<char>* gapMask);
    As soon as I saw those *, and the way the function was being called, all I could think about were pointers going out of scope, or pointers to secret local variables, or other weirdness.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM