473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Valgrind and STL

I'm working on creating a K-Nearest Neighbor classifier and having some problems that I can't figure out. I have a header file KNNClassifier.h and its class file KNNClassifier.cc

In KNNClassifier.h I have the following declarations:

Expand|Select|Wrap|Line Numbers
  1. class KNNClassifier{
  2. ...
  3. map<int, string> classNames;
  4. map<int, DataStore*> lastDataStores;
  5. map<int, int*> classificationMap;
  6. map<int, int> indexMap;
  7. vector<double> scaleVector;
  8. ...
  9. };
Everything worked fine until I added classificationMap and indexMap. After adding those two variables the program seg faults near the end of the KNNClassifier constructor. If I comment out either one of them (it doesn't matter which) the program will complete but Valgrind reports the same type of read/write errors but there is no seg fault. The relevant Valgrind output is below the constructor code snippet.

Expand|Select|Wrap|Line Numbers
  1. KNNClassifier::KNNClassifier(int numNeighbors)
  2. {
  3. ENTERCONSTRUCTOR;
  4.     statsInitialized = false;
  5.     classificationMatrix = NULL;
  6.     totalClassified = NULL;
  7.     classifiedCorrectly = NULL;
  8.     isTrained = false;
  9.     K = numNeighbors;
  10.     numVectors = 0;
  11.     maxVectors = 10;
  12.     trainedFeatures = (FeatureVector**)malloc(sizeof(FeatureVector*) * maxVectors);
  13. LEAVECONSTRUCTOR;
  14. }
The seg fault occurs at the last line when trainedFeatures is malloced in Valgrind but somewhere else when Valgrind is not running. Regardless I think the segfault is a symptom of the memory problems with STL.

==11646== 75 errors in context 53 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F07D: std::vector<double, std::allocator<double> >::begin() const (stl_vector.h:342)
==11646== by 0x805268D: std::vector<double, std::allocator<double> >::vector(std::vector<double, std::allocator<double> > const&) (stl_vector.h:234)
==11646== by 0x805695B: KNNClassifier::classifyFeatures(std::vector<double , std::allocator<double> >) (KNNClassifier.cc:436)
==11646== by 0x8057B95: KNNClassifier::classifyFromFile(char*, char*, char*) (KNNClassifier.cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_malloc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
==11646==
==11646== 75 errors in context 54 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F0A9: std::vector<double, std::allocator<double> >::end() const (stl_vector.h:360)
==11646== by 0x8052673: std::vector<double, std::allocator<double> >::vector(std::vector<double, std::allocator<double> > const&) (stl_vector.h:234)
==11646== by 0x805695B: KNNClassifier::classifyFeatures(std::vector<double , std::allocator<double> >) (KNNClassifier.cc:436)
==11646== by 0x8057B95: KNNClassifier::classifyFromFile(char*, char*, char*) (KNNClassifier.cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB14 is not stack'd, malloc'd or (recently) free'd
==11646==
==11646== 75 errors in context 55 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F0A9: std::vector<double, std::allocator<double> >::end() const (stl_vector.h:360)
==11646== by 0x804F134: std::vector<double, std::allocator<double> >::size() const (stl_vector.h:402)
==11646== by 0x805261F: std::vector<double, std::allocator<double> >::vector(std::vector<double, std::allocator<double> > const&) (stl_vector.h:233)
==11646== by 0x805695B: KNNClassifier::classifyFeatures(std::vector<double , std::allocator<double> >) (KNNClassifier.cc:436)
==11646== by 0x8057B95: KNNClassifier::classifyFromFile(char*, char*, char*) (KNNClassifier.cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB14 is not stack'd, malloc'd or (recently) free'd
==11646==
==11646== 75 errors in context 56 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F07D: std::vector<double, std::allocator<double> >::begin() const (stl_vector.h:342)
==11646== by 0x804F119: std::vector<double, std::allocator<double> >::size() const (stl_vector.h:402)
==11646== by 0x805261F: std::vector<double, std::allocator<double> >::vector(std::vector<double, std::allocator<double> > const&) (stl_vector.h:233)
==11646== by 0x805695B: KNNClassifier::classifyFeatures(std::vector<double , std::allocator<double> >) (KNNClassifier.cc:436)
==11646== by 0x8057B95: KNNClassifier::classifyFromFile(char*, char*, char*) (KNNClassifier.cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_malloc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
==11646==
==11646== 216 errors in context 57 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F44A: __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >::__normal_iterator(double* const&) (stl_iterator.h:650)
==11646== by 0x804F4C4: std::vector<double, std::allocator<double> >::begin() (stl_vector.h:333)
==11646== by 0x805242D: std::vector<double, std::allocator<double> >::operator[](unsigned) (stl_vector.h:480)
==11646== by 0x80550B2: KNNClassifier::calcFeatureScale() (KNNClassifier.cc:706)
==11646== by 0x8058479: KNNClassifier::trainFromFile(char*, char*) (KNNClassifier.cc:185)
==11646== by 0x805CC93: main (main.cc:65)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_malloc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
May 30 '07 #1
7 10902
weaknessforcats
9,208 Expert Mod 8TB
This is not a good idea:

Expand|Select|Wrap|Line Numbers
  1. map<int, DataStore*> lastDataStores;
  2. map<int, int*> classificationMap;
  3.  
You have no idea of the pointers are valid when you use them. STL Containers tend to move things around and the container may delete your DataStore* which will delete the object and make the copy invalid.

I recommend that you use handles in your containers instead of pointers. If you are unsure what I am talkin about, see my article on Handle classes in the C++ Articles section of this web site.

Also, stop using malloc(). Constructors are not called so your allocation has not been initialized. malloc() and its ilk should never be used in STL and never in C++ either, for that matter.
May 30 '07 #2
I realize that malloc and pointers are unsafe. However, errors are being reported in the constructor before the vector/maps are even touched so that shouldn't really be the issue here should it?
May 30 '07 #3
Ok, so I've updated the code to eliminate the pointers and malloc. Now I have


Expand|Select|Wrap|Line Numbers
  1. class KNNClassifier
  2. {
  3. ...
  4. vector<FeatureVector> trainedFeatures;
  5. ...
  6. map<int, string> classNames;
  7. map<int, DataStore> lastDataStores;
  8. map<int, vector<int> > classMap;
  9. map<int, int> indexMap;
  10.  
  11. vector<double> scaleVector;
  12. ...
  13. };
and the constructor now looks like this:

Expand|Select|Wrap|Line Numbers
  1. KNNClassifier::KNNClassifier(int numNeighbors)
  2. {
  3. ENTERCONSTRUCTOR;
  4.     /*
  5.      * Initialize all classifier parameters
  6.      */
  7.     statsInitialized = false;
  8.     classificationMatrix = NULL;
  9.     totalClassified = NULL;
  10.     classifiedCorrectly = NULL;
  11.     isTrained = false;
  12.     K = numNeighbors;
  13.     numVectors = 0;
  14.     maxVectors = 10;
  15. LEAVECONSTRUCTOR;
  16. }
I'm still getting the same seg fault and errors reported by Valgrind. The errors appear to be happening during template instantiation. I've included a couple of the errors from Valgrind but there is one of these errors for each template in the header.

==6420== Invalid write of size 4
==6420== at 0x805A414: std::_Rb_tree<int, std::pair<int const, std::vector<int, std::allocator<int> > >, std::_Select1st<std::pair<int const, std::vector<int, std::allocator<int> > > >, std::less<int>, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > >::_Rb_tree_impl<std::less<int>, false>::_Rb_tree_impl(std::allocator<std::_Rb_tree _node<std::pair<int const, std::vector<int, std::allocator<int> > > > > const&, std::less<int> const&) (stl_tree.h:407)
==6420== by 0x805A47A: std::_Rb_tree<int, std::pair<int const, std::vector<int, std::allocator<int> > >, std::_Select1st<std::pair<int const, std::vector<int, std::allocator<int> > > >, std::less<int>, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > >::_Rb_tree(std::less<int> const&, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > const&) (stl_tree.h:562)
==6420== by 0x805A4B1: std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > >::map() (stl_map.h:157)
==6420== by 0x805703E: KNNClassifier::KNNClassifier(int) (KNNClassifier.cc:31)
==6420== by 0x805DAF6: main (main.cc:55)
==6420== Address 0x567EB0C is 8 bytes after a block of size 100 alloc'd
==6420== at 0x4021DC5: operator new(unsigned) (vg_replace_malloc.c:163)
==6420== by 0x805DAE0: main (main.cc:55)
==6420==
==6420== Invalid write of size 4
==6420== at 0x805A438: std::_Rb_tree<int, std::pair<int const, std::vector<int, std::allocator<int> > >, std::_Select1st<std::pair<int const, std::vector<int, std::allocator<int> > > >, std::less<int>, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > >::_Rb_tree_impl<std::less<int>, false>::_Rb_tree_impl(std::allocator<std::_Rb_tree _node<std::pair<int const, std::vector<int, std::allocator<int> > > > > const&, std::less<int> const&) (stl_tree.h:411)
==6420== by 0x805A47A: std::_Rb_tree<int, std::pair<int const, std::vector<int, std::allocator<int> > >, std::_Select1st<std::pair<int const, std::vector<int, std::allocator<int> > > >, std::less<int>, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > >::_Rb_tree(std::less<int> const&, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > const&) (stl_tree.h:562)
==6420== by 0x805A4B1: std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<int const, std::vector<int, std::allocator<int> > > > >::map() (stl_map.h:157)
==6420== by 0x805703E: KNNClassifier::KNNClassifier(int) (KNNClassifier.cc:31)
==6420== by 0x805DAF6: main (main.cc:55)
==6420== Address 0x567EB04 is 0 bytes after a block of size 100 alloc'd
==6420== at 0x4021DC5: operator new(unsigned) (vg_replace_malloc.c:163)
==6420== by 0x805DAE0: main (main.cc:55)
May 31 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
This is not correct:

I realize that malloc and pointers are unsafe. However, errors are being reported in the constructor before the vector/maps are even touched so that shouldn't really be the issue here should it?
By the time you get to the { of your constructor, the maps have already been allocated and their default constructors called.

I compiled this code with no errors using Visual Studio.NET 2005

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <map>
  5.  
  6. class DataStore
  7. {
  8.  
  9. };
  10. class FeatureVector
  11. {
  12.  
  13. };
  14.  
  15. class KNNClassifier
  16. {
  17. //...
  18. vector<FeatureVector> trainedFeatures;
  19. //...
  20. map<int, string> classNames;
  21. map<int, DataStore> lastDataStores;
  22. map<int, vector<int> > classMap;
  23. map<int, int> indexMap;
  24.  
  25. vector<double> scaleVector;
  26. //...
  27. };
  28.  
  29. int main()
  30. {
  31.      KNNClassifier  k;
  32. }
  33.  
There's something else going on.

I will be more than happy to add your KNNClassifier constructor if you tell me what the data members are that you initialize in your constructor.
May 31 '07 #5
This is not correct:



By the time you get to the { of your constructor, the maps have already been allocated and their default constructors called.

I compiled this code with no errors using Visual Studio.NET 2005
My problem wasn't in compilation, it was a run time error. I actually managed to get the problem solved but it still doesn't make sense to me why it was manifesting during the constructor. There was a double free towards the end of my program long after the constructor had been called.
May 31 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
Consider using handles.

You can never delete in a C++ program unless you know you have the last pointer to that resource. That means the single most dangerous thing you can do in C++ is pass pointers around.

See my article on Handle Classes in the C/C++ Articles section.
May 31 '07 #7
wkailey
1 Bit
This is an STL bug, IMO.
Aug 10 '22 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jerald | last post by:
Running python 2.3.4 on valgrind (a tool like purify which checks the use of uninitialized memory, etc), gives a lot of errors. See below. jfj@cluster:~/> python -V Python 2.3.4...
2
by: Andreas Andersen | last post by:
Hi I have been using valgrind to debug a program I have written in C++ and it has been very helpful in eliminating memory errors/leaks. However, I just recently discovered a bug and from what I...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
5
by: Roman Mashak | last post by:
Hello, All! I already posted my question and received valuable feedbacks, I changed my code as was proposed here but still receive the same error of valgrind. SO, the code is: #define...
1
by: Nathan Bates | last post by:
Are the Python developers running Python under Valgrind? If not, FYI, Valgrind is a excellent memory-checker for Linux. Valgrind is reporting a ton of memory problems. Worrisome are "Conditional...
1
by: mfried | last post by:
Hi all, I'm testing an application using valgrind 3.2.0 unter Linux kernel 2.4. When the test are finished and the app terminates valgrind gives the error: --15631:0:aspacem Valgrind: FATAL:...
4
by: Simple Simon | last post by:
It's entirely possible that I'm completely in the wrong line of work, but I don't see what Valgrind is complaining about in this code. Invalid read/writes? If I free the within main(), rather...
0
by: Salvatore Iovene | last post by:
Hi, I'm using the pexpect module to spawn some processes in a testing environment. pexpect uses, internally, os.execv. What I need to do is valgrind python and get memory leaks from the spawned...
2
by: tvnaidu | last post by:
valgrind error - "failed to start tool 'memcheck' for platform x86-linux" no such file or dir copied all lib and bin files which are generated after I compiled valgrind package, when I run my exe...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.