函数的返回值已损坏 我在Visual C++ 2008中有一个我以前没有见过的情况。我有一个包含4个STL对象(精确地说是列表和向量)和整数的类

函数的返回值已损坏 我在Visual C++ 2008中有一个我以前没有见过的情况。我有一个包含4个STL对象(精确地说是列表和向量)和整数的类,c++,visual-c++,C++,Visual C++,它有一个方法: inline int id() { return m_id; } 此方法的返回值已损坏,我不知道原因 我想相信这是一个堆栈崩溃,但据我所知,我没有缓冲区溢出或分配问题 更多观察结果 有件事让我很反感。调试器在提到的位置打印正确的值//错误的ID m_header = new DnsHeader(); assert(_CrtCheckMemory()); if (m_header->init(bytes, size)) { eprintf("0

它有一个方法:

inline int id() { return m_id; }
此方法的返回值已损坏,我不知道原因

我想相信这是一个堆栈崩溃,但据我所知,我没有缓冲区溢出或分配问题


更多观察结果 有件事让我很反感。调试器在提到的位置打印正确的值//错误的ID

  m_header = new DnsHeader();
  assert(_CrtCheckMemory());

  if (m_header->init(bytes, size))
  {
    eprintf("0The header ID is %d\n", m_header->id()); // wrong ID!!!
内部m_头->初始化()

m_header->id()是头文件中的内联函数

inline int id() { return m_id; }

我真的不知道如何最好地发布我的代码片段,但这里是我最好的尝试。如果不足,请告知我:

DnsHeader
DnsPacket
内部有一个对象
m_头

主体:

DnsPacket *p ;
p = new DnsPacket(r);
assert (_CrtCheckMemory());
p->add_bytes(buf, r); // add bytes to a vector m_bytes inside DnsPacket
if (p->parse())
{
read_packet(sin, *p);
}
size_t size = m_bytes.size(); // m_bytes is a vector
  unsigned char *bytes = new u_char[m_bytes.size()];
  copy(m_bytes.begin(), m_bytes.end(), bytes); 

m_header = new DnsHeader();
  eprintf("m_header allocated at %x\n", m_header);
  assert(_CrtCheckMemory());
  if (m_header->init(bytes, size)) // just set the ID and a bunch of other ints here.
{
    size_t pos = DnsHeader::SIZE; // const int
    if (pos != size)
      ; // XXX perhaps generate a warning about extraneous data?

    if (ok)
      m_parsed = true;

  }
  else
  {
    m_parsed = false;
  }

  if (!ok) {
    m_parsed = false;
  }
  return m_parsed;
}
  DnsHeader& h = p.header();
  eprintf("The header ID is %d\n", h.id()); // ID is wrong here
...
m_id = -1;
  m_qdcount = m_ancount = m_nscount = m_arcount = 0;

  memset(&m_flags, 0, sizeof(m_flags)); // m_flags is a struct
  m_flags.rd = 1;
return *m_header;
p->parse:

DnsPacket *p ;
p = new DnsPacket(r);
assert (_CrtCheckMemory());
p->add_bytes(buf, r); // add bytes to a vector m_bytes inside DnsPacket
if (p->parse())
{
read_packet(sin, *p);
}
size_t size = m_bytes.size(); // m_bytes is a vector
  unsigned char *bytes = new u_char[m_bytes.size()];
  copy(m_bytes.begin(), m_bytes.end(), bytes); 

m_header = new DnsHeader();
  eprintf("m_header allocated at %x\n", m_header);
  assert(_CrtCheckMemory());
  if (m_header->init(bytes, size)) // just set the ID and a bunch of other ints here.
{
    size_t pos = DnsHeader::SIZE; // const int
    if (pos != size)
      ; // XXX perhaps generate a warning about extraneous data?

    if (ok)
      m_parsed = true;

  }
  else
  {
    m_parsed = false;
  }

  if (!ok) {
    m_parsed = false;
  }
  return m_parsed;
}
  DnsHeader& h = p.header();
  eprintf("The header ID is %d\n", h.id()); // ID is wrong here
...
m_id = -1;
  m_qdcount = m_ancount = m_nscount = m_arcount = 0;

  memset(&m_flags, 0, sizeof(m_flags)); // m_flags is a struct
  m_flags.rd = 1;
return *m_header;
读取数据包:

DnsPacket *p ;
p = new DnsPacket(r);
assert (_CrtCheckMemory());
p->add_bytes(buf, r); // add bytes to a vector m_bytes inside DnsPacket
if (p->parse())
{
read_packet(sin, *p);
}
size_t size = m_bytes.size(); // m_bytes is a vector
  unsigned char *bytes = new u_char[m_bytes.size()];
  copy(m_bytes.begin(), m_bytes.end(), bytes); 

m_header = new DnsHeader();
  eprintf("m_header allocated at %x\n", m_header);
  assert(_CrtCheckMemory());
  if (m_header->init(bytes, size)) // just set the ID and a bunch of other ints here.
{
    size_t pos = DnsHeader::SIZE; // const int
    if (pos != size)
      ; // XXX perhaps generate a warning about extraneous data?

    if (ok)
      m_parsed = true;

  }
  else
  {
    m_parsed = false;
  }

  if (!ok) {
    m_parsed = false;
  }
  return m_parsed;
}
  DnsHeader& h = p.header();
  eprintf("The header ID is %d\n", h.id()); // ID is wrong here
...
m_id = -1;
  m_qdcount = m_ancount = m_nscount = m_arcount = 0;

  memset(&m_flags, 0, sizeof(m_flags)); // m_flags is a struct
  m_flags.rd = 1;
return *m_header;
DnsHeader构造函数:

DnsPacket *p ;
p = new DnsPacket(r);
assert (_CrtCheckMemory());
p->add_bytes(buf, r); // add bytes to a vector m_bytes inside DnsPacket
if (p->parse())
{
read_packet(sin, *p);
}
size_t size = m_bytes.size(); // m_bytes is a vector
  unsigned char *bytes = new u_char[m_bytes.size()];
  copy(m_bytes.begin(), m_bytes.end(), bytes); 

m_header = new DnsHeader();
  eprintf("m_header allocated at %x\n", m_header);
  assert(_CrtCheckMemory());
  if (m_header->init(bytes, size)) // just set the ID and a bunch of other ints here.
{
    size_t pos = DnsHeader::SIZE; // const int
    if (pos != size)
      ; // XXX perhaps generate a warning about extraneous data?

    if (ok)
      m_parsed = true;

  }
  else
  {
    m_parsed = false;
  }

  if (!ok) {
    m_parsed = false;
  }
  return m_parsed;
}
  DnsHeader& h = p.header();
  eprintf("The header ID is %d\n", h.id()); // ID is wrong here
...
m_id = -1;
  m_qdcount = m_ancount = m_nscount = m_arcount = 0;

  memset(&m_flags, 0, sizeof(m_flags)); // m_flags is a struct
  m_flags.rd = 1;
return *m_header;
p.header():

DnsPacket *p ;
p = new DnsPacket(r);
assert (_CrtCheckMemory());
p->add_bytes(buf, r); // add bytes to a vector m_bytes inside DnsPacket
if (p->parse())
{
read_packet(sin, *p);
}
size_t size = m_bytes.size(); // m_bytes is a vector
  unsigned char *bytes = new u_char[m_bytes.size()];
  copy(m_bytes.begin(), m_bytes.end(), bytes); 

m_header = new DnsHeader();
  eprintf("m_header allocated at %x\n", m_header);
  assert(_CrtCheckMemory());
  if (m_header->init(bytes, size)) // just set the ID and a bunch of other ints here.
{
    size_t pos = DnsHeader::SIZE; // const int
    if (pos != size)
      ; // XXX perhaps generate a warning about extraneous data?

    if (ok)
      m_parsed = true;

  }
  else
  {
    m_parsed = false;
  }

  if (!ok) {
    m_parsed = false;
  }
  return m_parsed;
}
  DnsHeader& h = p.header();
  eprintf("The header ID is %d\n", h.id()); // ID is wrong here
...
m_id = -1;
  m_qdcount = m_ancount = m_nscount = m_arcount = 0;

  memset(&m_flags, 0, sizeof(m_flags)); // m_flags is a struct
  m_flags.rd = 1;
return *m_header;
m\u头->初始化:(u\u字符*字节,整数大小)


您似乎在使用指向无效类的指针。显示的返回值是VS通常用于初始化内存的值:

2^32 - 842150451 = 0xCDCDCDCD

您似乎在使用指向无效类的指针。显示的返回值是VS通常用于初始化内存的值:

2^32 - 842150451 = 0xCDCDCDCD
  • 您可能尚未初始化此函数所属的类
  • 在上下文中看不到更多的代码。。可能是m_id超出了您预期的范围
  • 您可能尚未初始化此函数所属的类
  • 在上下文中看不到更多的代码。。可能是m_id超出了您预期的范围

  • 重新安装VC++。一切都解决了


    感谢您的时间和支持谢谢你

    重新安装VC++。一切都解决了


    感谢您的时间和支持谢谢你

    什么时候设置m_id?你能发布更多的代码片段吗?您的代码中是否存在并发性?这里的信息太少,您应该显示更多的代码,否则这只是猜测。首先想到的是你在一个被破坏的对象上调用这个方法。著名的最后一句话:“据我所知,我没有缓冲区溢出或分配问题”:)什么时候设置m_id?你能发布更多的代码片段吗?您的代码中是否存在并发性?这里的信息太少,您应该显示更多的代码,否则这只是猜测。首先想到的是,您正在对一个已销毁的对象调用该方法。著名的最后一句话:“据我所知,我没有缓冲区过度运行或分配问题”:)严格地说,VS将使用0xcd初始化堆分配内存,使用0xdd初始化堆释放内存,使用0xcc初始化堆栈内存。哇!很快回复!谢谢你!我还得到(似乎)随机值,比如1573768653(0x5DCD)、1379021363(0x52323233)、1389219277(ox52cdcdcd)。然而,让我感到不快的是,当我将函数从内联改为非内联时,一切都很好@tdsmapper:根据给定的信息,我不确定发生了什么。如果您能提供一个简短的代码示例来演示这个问题,它可能会有所帮助。有人可能会在几秒钟内发现问题(至少这是stackoverflow上通常发生的情况)。严格来说,VS将使用0xcd初始化堆分配内存,使用0xdd初始化堆释放内存,使用0xcc初始化堆栈内存。哇!很快回复!谢谢你!我还得到(似乎)随机值,比如1573768653(0x5DCD)、1379021363(0x52323233)、1389219277(ox52cdcdcd)。然而,让我感到不快的是,当我将函数从内联改为非内联时,一切都很好@tdsmapper:根据给定的信息,我不确定发生了什么。如果您能提供一个简短的代码示例来演示这个问题,它可能会有所帮助。有人可能会在几秒钟内发现问题(至少这是stackoverflow上通常发生的情况)。