Thread: Problems when destroying a vector of strings

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    MD
    Posts
    10

    Unhappy Problems when destroying a vector of strings

    I have 2 vectors of strings. I'm passing them to a function by reference with one being const inside the function. I add some strings to the non-const vector and come out of the function. For some reason when the vector that was not const goes out of scope it throws exceptions when trying to deallocate the strings. Here is an example.
    Code:
    #include <vector>
    #include <string>
    using std::string;
    using std::vector;
    
    void func1(const vector<string> &vector1, vector<string> &vector2)
    {
    //it doesn't matter what happens to vector1 because it is working properly //disregard it for this example vector2.clear(); //to make sure it's empty //push on some random data for this example //I'm actually grabbing some text from an access DB for (int i = 0; i < 8; i++) {
    vector2.push_back("Go Time!");
    }
    } int main() {
    vector<string> vector1; vector<string> vector2; func1(vector1, vector2); //do something with vector 2 now, only reading from it though, no writing //I was letting them go out of scope but I couldn't tell which vector was having trouble //The problem manifests when clear() is called as well vector1. clear(); vector2.clear(); //here is where the program breaks return 1;
    }
    It doesn't seem matter what text is in the vector. I've tried rearranging the order of the string but it always seems to break on the second or third element. I've tried doing a pop_back() from the end of the vector and it goes fine until it gets up toward the beginning of the vector.

    Just for reference I'm using Visual C++ 6.0 (ugh) and vector1 destroys just fine. The actual exception is happening when in the std::string's deallocate function. It is throwing an assert on IsValidHeapPointer.

    Thanks in advance for any help.
    Last edited by ryanrigdon; 08-14-2009 at 08:25 AM. Reason: Just to remove a typo/programmer error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String problems - strings seem to join?
    By Metalixia in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2007, 11:00 AM
  2. strings in C++
    By elad in forum C++ Programming
    Replies: 11
    Last Post: 05-20-2006, 02:27 AM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Getting the number of strings in a STRINGTABLE resource
    By eth0 in forum Windows Programming
    Replies: 1
    Last Post: 09-30-2005, 02:57 AM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM

Tags for this Thread