Results 1 to 17 of 17

Thread: Problems passing an array of pointers to objects to a function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    Please show us the piece of code ;-)
    (The error probably is either on of: 1) the variable was not initialized, 2) does not point to memory allocated on the heap, 3) it is on the heap, but not an array, 4) double free. Hard to guess, though.)

  2. #2
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    Heh, I didn't expect you to guess though The complete code is available in the link in the first post: http://code.google.com/p/mll/source/checkout

    It's not a terrible lot though. The place where it is going wrong is where I delete[] name and material_path, although the corruption is probably happening before that.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    you create a vector of num_materials elements, and then add with push_back. That way you end up with a vector of 2x num_materials elements, I think.
    Probably you want to call reserve, instead.

    side note:
    Qt Code:
    1. if( materials != NULL )
    2. delete materials;
    To copy to clipboard, switch view to plain text mode 
    These checks are not useful. Don't hurt either, though.

  4. #4
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    Quote Originally Posted by caduel View Post
    you create a vector of num_materials elements, and then add with push_back. That way you end up with a vector of 2x num_materials elements, I think.
    Probably you want to call reserve, instead.

    side note:
    Qt Code:
    1. if( materials != NULL )
    2. delete materials;
    To copy to clipboard, switch view to plain text mode 
    These checks are not useful. Don't hurt either, though.
    I thought that calling delete on a variable that has been set to NULL causes a segfault? I pass a lot of variables into the function set to NULL by default, since not every variable has to be defined in an OBJ file.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    You allocate materials as a pointer to std::vector, but free it as an array with delete[].
    Change that to delete and you should be ok.

    HTH

  6. #6
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    Quote Originally Posted by caduel View Post
    You allocate materials as a pointer to std::vector, but free it as an array with delete[].
    Change that to delete and you should be ok.

    HTH
    You lost me. I don't allocate anything as a vector. Could you point to the file and line that that happens according to you?

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    i) delete NULL is absolutely fine (just as free(NULL))

    ii) When I browsed your svn repository online, I found such a line.
    But it is not there. Perhaps I somehow git into an older revision. Sorry.

    Any issues left?

  8. #8
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    Well, the program is still crashing on some files. I don't know why though, it does it when I call delete <var>. It differs per file. Sometimes it happens when I am deleting the normal_idx array, etc. I hesitate to use vectors, since with many allocations of models it would, I feel, introduce unwelcome memory usage - especially since there is no need to dynamically grow the arrays.

    I'm trying to track it down now, although it may take a while. I'm learning a lot from it though, so I don't really mind too much, as long as it doesn't take too long

  9. #9
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Problems passing an array of pointers to objects to a function

    Oh God, what a stupid mistake

    Qt Code:
    1. else if( buffer.substr( 0, 1 ) == "o" ){
    2. name = new char[buffer.length() - 2];
    3. sscanf(buffer.c_str(), "o %s", name);
    To copy to clipboard, switch view to plain text mode 

    I was allocating like that for certain variables. But I was not allowing space for the terminating \0 like that. It should have been buffer.length() - 1, obviously.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52

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
  •  
Qt is a trademark of The Qt Company.