Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: valgrind and vectors

  1. #1
    Join Date
    Feb 2009
    Beans
    202

    valgrind and vectors

    Getting whole bunch of invalid read/write size 4 error messages for following line. Not just one but tons for every one such call completely flooding error lines. What's up with that?

    vector<myClass*> myVector;

    int objectCount=someNumber;

    myVector.resize(objectCount);

    the resize is where valgrind reports those invalid read/write size errors. What's up with that? Valgrind having problems with vector? Me doing something wrong here? I can't figure out what the bloody heck is wrong with simple resize of vector that hasn't even been used yet?

  2. #2
    Join Date
    May 2006
    Beans
    1,790

    Re: valgrind and vectors

    Quote Originally Posted by tneva82 View Post
    Getting whole bunch of invalid read/write size 4 error messages for following line. Not just one but tons for every one such call completely flooding error lines. What's up with that?

    vector<myClass*> myVector;

    int objectCount=someNumber;

    myVector.resize(objectCount);

    the resize is where valgrind reports those invalid read/write size errors. What's up with that? Valgrind having problems with vector? Me doing something wrong here? I can't figure out what the bloody heck is wrong with simple resize of vector that hasn't even been used yet?
    Has it even been set yet? What if you do

    Code:
    static vector<myClass*> myVector;
    ?

  3. #3
    Join Date
    Feb 2009
    Beans
    202

    Re: valgrind and vectors

    Quote Originally Posted by Arndt View Post
    Has it even been set yet? What if you do

    Code:
    static vector<myClass*> myVector;
    ?
    Hmm. Not sure if I want it to be static. If it's static there's just one vector for that class containing that vector shared by all the instances of that class right? Each instance should have their own unique myVector. Would change logic of program a "bit" if there's shared myVector.

  4. #4
    Join Date
    Oct 2004
    Beans
    266
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: valgrind and vectors

    It's probably because you have allocated a reference to the vector-object, but you have not allocated the actual vector-object! You are essentially trying to ask a null-reference to do something....

    Assuming its c++:
    Code:
    vector<myClass*> myVector; // This just allocates a pointer to a vector object
    
    myVector = new vector<myClass*>(); // This would allocate the actual object,. and ofcourse assign the objects address to the pointer...
    
    int objectCount=someNumber;
    
    // Now, you can call the method of the object (without line 2 above, there is no object to receive the call!
    myVector.resize(objectCount);

    I think this is what Arndt meant with "Has it even been set yet?", also the actual output from valgrind would've been interesting to see....


    /N
    Last edited by WakkiTabakki; April 6th, 2009 at 08:26 AM.

  5. #5
    Join Date
    May 2006
    Beans
    1,790

    Re: valgrind and vectors

    Quote Originally Posted by tneva82 View Post
    Hmm. Not sure if I want it to be static. If it's static there's just one vector for that class containing that vector shared by all the instances of that class right? Each instance should have their own unique myVector. Would change logic of program a "bit" if there's shared myVector.
    Yes, I'm not suggesting that it's the correct solution, only wondering if it will make valgrind happy, as a step towards understanding the problem. Sorry for being too terse.

  6. #6
    Join Date
    Feb 2009
    Beans
    202

    Re: valgrind and vectors

    Quote Originally Posted by WakkiTabakki View Post
    Code:
    vector<myClass*> myVector; // This just allocates a pointer to a vector object
    
    myVector = new vector<myClass*>(); // This would allocate the actual object,. and ofcourse assign the objects address to the pointer...
    Well here's what I attempted:
    Code:
    client.h:
    
    class client {
    private:
      vector<object3d*> partMasterList;
    };
    
    clientBasic.cpp:
    
    client::setPartList() {
      partMasterList = new vector<object3d*>();   
    }
    and here's what compiler gave me:

    Code:
    g++ src/clientBasic.cpp -g -c -o obj/clientBasic.o -lGL -lSDL -lGLU -lSDL_net `sdl-config --cflags` `pkg-config --cflags ftgl`
    src/clientBasic.cpp: In member function ‘void client::setPartList(const char*)’:
    src/clientBasic.cpp:213: error: no match for ‘operator=’ in ‘((client*)this)->client::partMasterList = (((std::vector<object3d*, std::allocator<object3d*> >*)operator new(12u)), (<statement>, <anonymous>))’
    /usr/include/c++/4.3/bits/vector.tcc:144: note: candidates are: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = object3d*, _Alloc = std::allocator<object3d*>]
    make: *** [obj/clientBasic.o] Error 1
    Can you even call new to non-pointer variables like in that vector allocator? I thought that was for dynamic memory allocation which would require pointers which myVector isn't though might be wrong.

    Note that object3d has constructor which doesn't take parameters(though I only use one with parameters myself).

    Oh and totally unrelated but have to vent a bit: STUPID WAVEFRONT! You have all the letters available and yet have to put 2 different formats to one letter in .obj file? Gee. Wondered why there's odd outputs in my converter which turns bunch of .obj files to own file format(that gets rid of few redundant data which previous files from animation gave me already and packs full animation to one file. Nothing fancy but just bit of restructuring of data to be more suitable for my needs). Well I looked at .obj file more closely and sure enough after whole bunch of f x/x/x x/x/x x/x/x lines there was f x x line. Gee. What the heck is that doing there? Now I need to figure out how to filter those ones out. No wonder output was all weird...Couldn't they have implemented line(I presume that's line and numbers are vertex and vertex) to be under different letter like l? Would sure make things easier!

    (of course I COULD just edit manually those offending lines but 2x49+4x29 lines to be removed across LARGE files...Uhhuh. And this for simple objects. Not good. More preferably converter should do that thing automaticly).

    Edit: And elsewhere. How on earth can one get double free or corruption error message when deleting non-NULL pointer?

    Code:
    object3d::~object3d() {
      if(myFrames!=NULL) {
        for(int i=0;i<frameCount;i++) {
          if(myFrames!=NULL) {
    	printf("myFrames isn't NULL!\n");
          }
          if(myFrames[i].vertexTable != NULL) {
    	printf("vertex table isn't NULL!\n");
          }
          if(myFrames[i].vertexTable != NULL) delete[] myFrames[i].vertexTable;
          if(myFrames[i].normalTable != NULL) delete[] myFrames[i].normalTable;
          if(myFrames[i].normalIndexes != NULL) delete[] myFrames[i].normalIndexes;
        }
    
        if(myFrames!=NULL) delete[] myFrames;
        if(myPolygons!=NULL) delete[] myPolygons;
        if(uMapTable!=NULL) delete[] uMapTable;
        if(vMapTable!=NULL) delete[] vMapTable;
      }
    }
    Goes haywire on trying to delete vertexTable. myFrames is frame *myFrames; and frame is:

    Code:
    struct frame {
      vertex *vertexTable;
      normalVector *normalTable;
      int *normalIndexes;
      int normalCount;
    };
    (vertex is just struct with 3 float values).

    Only one place in code where object3d classes gets destroyed(destructor of client which loops through vector containing these objects deleting them if not NULL) so there shouldn't be any rogue destructor calls as far as I can tell. And unless I'm mistaken those if clauses should prevent any attempt to delete NULL pointers.

    Even more weird is that other section of code that works pretty much identically works...Go figure. Variable names change and location of functions change but that's it.

    edit: Found post in forum which suggested typing in console before running:

    export MALLOC_CHECK_=0

    and sure enough no crash then. Okay so anything I can do codewise to prevent this? Or do I have to wrap executable into script to ensure that's run before running? What's causing this that above prevents it? Is it just hiding symptoms? What's going on here?
    Last edited by tneva82; April 6th, 2009 at 10:32 AM.

  7. #7
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: valgrind and vectors

    Quote Originally Posted by tneva82 View Post
    Getting whole bunch of invalid read/write size 4 error messages for following line. Not just one but tons for every one such call completely flooding error lines. What's up with that?

    vector<myClass*> myVector;

    int objectCount=someNumber;

    myVector.resize(objectCount);

    the resize is where valgrind reports those invalid read/write size errors. What's up with that? Valgrind having problems with vector? Me doing something wrong here? I can't figure out what the bloody heck is wrong with simple resize of vector that hasn't even been used yet?
    I don't think that that's the problem. Here is an example program that works well:

    PHP Code:
    #include <vector>

    using namespace std;

    class 
    myClass {
        
    int h;
        
    int j;
    };


    int main() {
        
    vector<myClass*> myVector;

        
    int objectCount=100000;

        
    myVector.resize(objectCount);

    The reason why the code in your last post did not work is also quite simple: You tries to assign a pointer to a vector to a vector object. Drop the "new" and it should at least compile (you also might have to drop the "()" after the "vector<object3d*>").

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: valgrind and vectors

    Quote Originally Posted by WakkiTabakki View Post
    It's probably because you have allocated a reference to the vector-object, but you have not allocated the actual vector-object! You are essentially trying to ask a null-reference to do something....

    Assuming its c++:
    Code:
    vector<myClass*> myVector; // This just allocates a pointer to a vector object
    
    myVector = new vector<myClass*>(); // This would allocate the actual object,. and ofcourse assign the objects address to the pointer...
    
    int objectCount=someNumber;
    
    // Now, you can call the method of the object (without line 2 above, there is no object to receive the call!
    myVector.resize(objectCount);

    I think this is what Arndt meant with "Has it even been set yet?", also the actual output from valgrind would've been interesting to see....


    /N
    -1.

    The vector is not a pointer, and thus does not need to be allocated. The vector will contain pointers.

  9. #9
    Join Date
    Nov 2006
    Beans
    1,134

    Re: valgrind and vectors

    Quote Originally Posted by tneva82 View Post
    How on earth can one get double free or corruption error message when deleting non-NULL pointer?
    It happens when a pointer is not NULL and not pointing to a valid heap block. Check your pointer initialization code.

    Or, even better, use smart pointers and forget about deallocation. Oh well, I must have said that a dozen times already...

  10. #10
    Join Date
    Apr 2007
    Beans
    2,042

    Re: valgrind and vectors

    maybe valgrind is telling you that the contents (the pointers) are being lost or something when the vector is resized, try other methods like insert and push back

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •