Getting problem in std::vector<std::wstring>>

I want to know is there something wrong in passing in passing vector reference to a function as in the example below. This code is running well and nice. But the same type of code in my project gives me crash. I don't know why.

In that case whenever I calls the function which need std::vector & . then in the called function the size of the vector reaches some millionsss....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <string>

class A {
public:
    A() {}
    ~A() {}
    void GetVector(std::vector<std::wstring> &in) {
        std::wstring s = L"Hello";
        for(int i = 0; i < 10; i++)
            in.push_back(s);
    }
};

class B {
public:
    B() {}
    ~B() {}

    void GetData() {
        A a;
        std::vector<std::wstring> s;
        a.GetVector(s);
    }
};

int main() {
    B b;
    b.GetData();

    return 0;
}
Last edited on
code looks fine.
That code looks like it should work. We would really need to see the code that actually causes the crash to be able to help here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void SCPreferenceComp::PopulateComboBox()
{
    SCConfig *config = SCConfig::GetInstance();
    std::vector<std::wstring> languages;
    config->GetAllLangugesName(languages);
    for(size_t i = 0; i != languages.size(); i++)
        mLangListComboBox->addItem(languages[i].c_str(), i+1);
    if(mLangListComboBox->getNumItems() > 0)
        mLangListComboBox->setSelectedId(1);
}

bool SCConfig::GetAllLangugesName(std::vector<std::wstring> &outLangNames)
{
    bool retVal = false;
    do
    {
        if(!mXMLDoc)
            break;
        xercesc::DOMNodeList *langNodeList = mXMLDoc->getElementsByTagName(strToX("language"));
        if(!langNodeList)
            break;
        const XMLSize_t langCount = langNodeList->getLength();
        for(XMLSize_t i = 0; i < langCount; i++)
        {
            xercesc::DOMNode *curLangNode = langNodeList->item(i);
            if(!curLangNode)
                continue;
            xercesc::DOMElement *curLangElem = dynamic_cast<xercesc::DOMElement*>(curLangNode);
            if(!curLangElem)
                continue;
            wxString s = strToW(curLangElem->getAttribute(strToX("name")));
            outLangNames.push_back(s.c_str());
        }
        retVal = true;
    }while(false);

    return retVal;
}
This is the exact code where I am getting the run time crash....
when
 
SCConfig::GetAllLangugesName(std::vector<std::wstring> &outLangNames)


is called from
 
PopulateComboBox();


funtion. At the time of debugging I am finding that the size of vector ie, languages grows to some large value at the very first line.
Could you try removing all the xerces code from this function and then see. I doubt this is because of the xerces code here and nothing from std::vector.
Your do-while loop really adds nothing to the code- just executes once. So you might want to check that in case it's not what you intended. Next specifically where in SCConfig::GetAllLangugesName(std::vector<std::wstring> &outLangNames) does it cause a crash? and after how many iterations since you report getting the vector being populated.
Last edited on
Topic archived. No new replies allowed.