开发者

STL member variable initalization issue with windows API

开发者 https://www.devze.com 2022-12-30 14:29 出处:网络
I am creating a windows app that uses a vector of stings as a member variable.For some reason, I can compile but when it tries to get at any of 开发者_如何学运维the vectors members is crashes. the err

I am creating a windows app that uses a vector of stings as a member variable. For some reason, I can compile but when it tries to get at any of 开发者_如何学运维the vectors members is crashes. the error is 0xC0000005: Access violation reading location 0xcdcdcdd9. in the member function of the vector class.

this is the size() function where it breaks.

size_type capacity() const
{ // return current length of allocated storage
  return (this->_Myend - this->_Myfirst);
}

I am using visual studios 2010.

thank you Django


The problem is not with the STL code but with your code.

Since you didn't paste your code I'll show you an example of how you should be using a vector of strings.

std::vector<std::string> v;
v.push_back("hello");
v.push_back("world!");
//Note the above 2 string literals get implicitly converted to std::string
assert(v.size() == 2);
assert(v[0] == "hello");


I've come across this kind of thing before. Most likely cause is a corrupt heap.


in the header.

#include <iostream>
#include <string>
#include <vector>

using namespace std;


class Chat
{
protected:
    vector<string> m_buffers;

public:
    Chat();
    ~Chat();
};

in the cpp

Chat::Chat(){
    string init = "test";
    m_buffers.push_back(init); //crash
}
Chat::~Chat(){

}

sorry about the lack of code.... what wa I thinking (face palm)

0

精彩评论

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

关注公众号