Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ valgrind中大小为1的读取无效_C++_Valgrind_Invalidation - Fatal编程技术网

C++ valgrind中大小为1的读取无效

C++ valgrind中大小为1的读取无效,c++,valgrind,invalidation,C++,Valgrind,Invalidation,我正在运行一段有2个函数的代码,并运行valgrind,得到大小为1的无效读取。我们无法识别问题,请帮助 RrSSystemIntf_i::RrSSystemIntf_i() { RrXmlReader cfgReader; char* configFile = cfgFile; char* pss; U pssId; if (ROK != cfgReader.readConfig(configFile, (char*)"ABCD", (char*)"RR_NODES",

我正在运行一段有2个函数的代码,并运行valgrind,得到大小为1的无效读取。我们无法识别问题,请帮助

RrSSystemIntf_i::RrSSystemIntf_i()
{
  RrXmlReader cfgReader;
  char* configFile = cfgFile;
  char* pss;
  U pssId;

  if (ROK != cfgReader.readConfig(configFile, (char*)"ABCD", (char*)"RR_NODES",
                                  &pss)) {
    RR_ALERT("RrSSystemIntf_i: readConfig failed. exiting...");
    _exit();
  }

  pssId = atoi(pss);
}


int RrXmlReader::readConfig(char*& confFile, char* elem_type, char* val_type,
                            char** ret_val)
{
  reader.getValue(curContext, val_type, value);

  if (NULL == value) {
    RR_ALERT("XmlFactory::rrNodes : Config Error: %s missing from %s", val_type,
             elem_type);
    return RFAILED;
  }

  string returnStr;
  returnStr = std::string(value);
  *ret_val  = (char*)(returnStr.c_str());
  return ROK;
}
Valgrind跟踪:

==30007== Invalid read of size 1
==30007==    at 0x33296345CA: ____strtol_l_internal (in /lib64/libc-2.5.so)
==30007==    by 0x52D5A64: RrS7SystemIntf_i::RrS7SystemIntf_i() (stdlib.h:336)
==30007==    by 0x52AA9E7: RrObInit::initOb(int, char**) (RrObInit.cpp:360)
==30007==    by 0x52ACF6D: RrObInit::getInstance() (RrObInit.cpp:636)
==30007==    by 0x52AE909: tst (RrTst.cpp:515)
==30007==    by 0x4C12694: Init (gen.c:581)
==30007==    by 0x4C1135C: Main (mtss.c:484)
==30007==    by 0x52ADD7B: main (RrTst.cpp:225)
==30007==  Address 0x8e9dc28 is 24 bytes inside a block of size 28 free'd
==30007==    at 0x4A05743: operator delete(void*) (vg_replace_malloc.c:346)
==30007==    by 0x52FC330: RrXmlReader::readConfig(char*&, char*, char*, char**)    (basic_string.h:233)
==30007==    by 0x52D590B: RrS7SystemIntf_i::RrgS7SystemIntf_i() (RrS7System_i.cpp:325)

提前感谢您的帮助。

问题从以下行开始:

*ret_val  = (char*)(returnStr.c_str());
您正在通过
ret_val
返回一个地址,该地址在您从
readConfig
返回时将立即无效

Valgrind在执行以下操作时发现内存访问问题:

pssId = atoi(pss);
因为pss在该点指向无效内存

我的建议是:

readConfig
更改为

int RrXmlReader::readConfig(char*& confFile, char* elem_type, char* val_type,
                            std::string& ret_val)

您返回的是一个指向局部变量的指针,它永远不会有好的结果。
c_str()
返回的cstring只有在基
字符串
存在时才有效,函数返回后取消引用
ret_val
将是未定义的行为。与您的bug无关,但如果不打算修改C样式字符串,则应将其作为
char const*
,而不是丢弃const。这将有助于防止其他错误。这是一个相当稀疏的帖子。。。到目前为止,您是如何找到问题的?