开发者

Getting crash while calling a function which need reference to vector

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.... I have attached screenshot where I am actually getting this crash.

Getting crash while calling a function which need reference to vector

I just wants to know is there something wrong in these type of implementations...

#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;
}

Real code where I am getting the crash...

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;
}


I can't see anything wrong in that implementation other than the fact that it doesn't have any visible end result which leads me to believe it may not exactly match your failing code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜