-
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
-
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
-
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.
-
Has prfLength been initialized?
-
how is prfLength declared and initialized?
And what's the compiler you are using?
-
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.
-
-
Well, mask is actually a pointer to a vector<char>, not a vector.
-
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.
-
Have you tried writing the smallest and simplest program that demonstrates this problem?
-
In the very least, we need to know what is happening inside calcGapPenaltyMask... assuming that's where the resize is happening.
-
> 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 :rolleyes:
As laserlight says, post some code we can run which shows the problem.
Reposting "this line doesn't work" doesn't help.
-
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)
-
>> 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.
-
> 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.