Valgrind: Invalid read of size 8 error
The problem is most likely caused by the line:
vec.erase(v);
Using a range for loop and deleting an item from the container is not a good idea. Change your loop to:
for ( auto iter = vec.begin(); iter != vec.end(); /* Empty on purpose*/ )
{
if(domainList.find(iter->first) != domainList.end())
{
iter = vec.erase(iter);
}
else
{
++iter;
}
}
Valgrind reports invalid read size of 8, but there are no memory leaks
I figured the answer to my question. These two lines cause the problem:
i += 1;
interval = sequencearray[seqsize - i];
This can be fixed with a simple if statement that breaks out if it is invalid (condition being that seqsize - i < 0, it is invalid, and you break.
cpp - valgrind - Invalid read of size 8
Your first error says:
==3352== Invalid read of size 8
==3352== at 0x804CC8F: BOViL::math::Matrix<double>::operator*(BOViL::math::Matrix<double> const&) const (Matrix.h:285)
==3352== by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
==3352== by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix<double> const&, double) (ExtendedKalmanFilter.cpp:39)
==3352== by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
==3352== by 0x805266D: main (main.cpp:16)
==3352== Address 0x6a8b3c0 is 0 bytes after a block of size 48 alloc'd
==3352== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3352== by 0x804C986: BOViL::math::Matrix<double>::operator=(BOViL::math::Matrix<double> const&) (Matrix.h:224)
==3352== by 0x8051C62: BOViL::algorithms::ExtendedKalmanFilter::setUpEKF(BOViL::math::Matrix<double>, BOViL::math::Matrix<double>, BOViL::math::Matrix<double>) (ExtendedKalmanFilter.cpp:23)
==3352== by 0x804B74F: testSegmentation() (TestSegmentation.cpp:37)
==3352== by 0x805266D: main (main.cpp:16)
which in short means:
==3352== Invalid read of size 8
==3352== at 0x804CC8F: BOViL::math::Matrix<double>::operator*(BOViL::math::Matrix<double> const&) const (Matrix.h:285)
==3352== Address 0x6a8b3c0 is 0 bytes after a block of size 48 alloc'd
Knowing that your matrix is of double
, that means the array inside the matrix is allocated to contain 6 elements (48/sizeof double
). However, you are accessing 0 bytes after the block, which means you are accessing exactly element index 6.
So there are two things that you need to verify:
- Is 6 correct? Should the array contain 6 elements?
At line 285 of
Matrix.h
, which is likely inside thefor
loops, not here:Matrix<type_> mat(ptr, mRows, _mat.mCols); <<< ----- HERE
you need to examine what indices you are giving to the array. Likely, you will find the array being indexed at 6 and that's where you should figure out why.
Why do I get Invalid read of size 8? (Valgrind)
I finally found the source of the problem with the help of the hints @cdhowie gave. The member variable "chat" of the Message object caused the problem since as you can see here
if(doc.HasMember("chat"))
{
if(doc["chat"].IsObject())
chat = std::make_shared<Chat>(tools::Tools::get_json_as_string(doc["chat"]));
else
tools::Tools::write_err_log(Messages::field_does_not_contain_json_obj("chat"));
}
this part of the Message constructor only creates a chat instance if the passed HTTP response body contains the proper field. Therefore, this member access m_msgs_to_delete.at(m_msgs_to_delete.size() - 1)->chat->id
in line 40 of ChatCleaner.cpp called a null pointer as "chat" does not exist on the heap in this case.
PS: The object doc above comes from the library rapidjson. The definition is rapidjson::Document doc;
. Its purpose is parsing JSON objects and making data accessible. The whole source code is from my Telegram chat bot.
Valgrind Invalid read of size 8 and Address 0x5b7e520 is 0 bytes inside a block of size 16 free'd
Your List
class lacks a valid copy constructor and assignment operator but you are copying List
objects, for instance when you call sumNotReverse
.
This will result in a double free or a use after free, which is presumably the problem valgrind is complaining about.
See here for more details What is The Rule of Three?, or consult your favourite C++ book. Any decent C++ book is going to cover this topic.
Invalid read of size 8, Invalid write of size 8 (Valgrind)
std::cin >> rawScores[k];
studentInfo[j].score += rawScores[k]/scores[k] * weights[k];
From your above program j
and k
depends on the user input and hence their values can go beyond the actual array's studentInfo
rawScores
index.
Your program should have logic so that it your program does not access array bounds.
You may monitor your program
$ valgrind --tool=memcheck --db-attach=yes ./a.out
For detailed information about this concept and how to use it, you may refer following post:
https://stackoverflow.com/a/22658693/2724703
Valgrind invalid read of size 8 when printing out a character array
Code iterates past the 4 allocated pointers look for a null pointer.
while(*a != NULL){ // Line 232 when invalid read occurs
printf("%s\n", *a);
a++;
}
Code should allocate a 5th and assigned it NULL
.
// char **ptArray = calloc(4, sizeof(char *));
char **ptArray = calloc(5, sizeof *ptArray);
ptArray[0] = line1;
...
ptArray[3] = line4;
ptArray[4] = NULL;
Alternate, pass count
void printLinesN(char *ptArray[], size_t count){
for (size_t i = 0; i<count; i++) {
printf("%s\n", ptArray[i]);
}
}
// Call example
// printLines(ptArray);
printLinesN(ptArray, 4);
Valgrind: Invalid read of size 8, bytes after a block of size 8 alloc'd
At least this memory allocation
struct bmp_image* bmp_file = (struct bmp_image *)calloc(1, sizeof(struct bmp_image *));
is incorrect. It seems you mean
struct bmp_image* bmp_file = (struct bmp_image *)calloc(1, sizeof(struct bmp_image));
or
struct bmp_image* bmp_file = (struct bmp_image *)calloc(1, sizeof( *bmp_file));
Related Topics
How to Properly Link Libraries with Cmake
How to Check If an Object's Type Is a Particular Subclass in C++
C++ Sort Keeping Track of Indices
Cannot Call Member Function Without Object
C++ Return Value Without Return Statement
Choosing Between Std::Map and Std::Unordered_Map
When to Use Const and Const Reference in Function Args
How to Dynamically Declare an Array of Objects with a Constructor in C++
Initialize Parent's Protected Members with Initialization List (C++)
Which C++ Standard Is the Default When Compiling with G++
Search for a Struct Item in a Vector by Member Data
Multithreaded Rendering on Opengl