简体   繁体   English

我的记忆怎么了?

[英]What's going on with my memory?

I have a function that allocated a buffer for the size of a file with 我有一个函数,它为文件的大小分配了一个缓冲区

char *buffer = new char[size_of_file];

The i loop over the buffer and copy some of the pointers into a subbuffer to work with smaller units of it. i在缓冲区上循环,然后将一些指针复制到子缓冲区中,以使用较小的单元。

char *subbuffer = new char[size+1];

for (int i =0; i < size; i++) {
  subbuffer[i] = (buffer + cursor)[i];
}

Next I call a function and pass it this subbuffer, and arbitrary cursor for a location in the subbuffer, and the size of text to be abstracted. 接下来,我调用一个函数,并将其传递给此子缓冲区,并向其传递该子缓冲区中某个位置的任意游标以及要提取的文本的大小。

  wchar_t* FileReader::getStringForSizeAndCursor(int32_t size, int cursor, char *buffer) {

  int wlen = size/2;

  #if MARKUP_SIZEOFWCHAR == 4 // sizeof(wchar_t) == 4
  uint32_t *dest = new uint32_t[wlen+1];
  #else
  uint16_t *dest = new uint16_t[wlen+1];
  #endif

  char *bcpy = new char[size];
  memcpy(bcpy, (buffer + cursor), size+2);



  unsigned char *ptr = (unsigned char *)bcpy; //need to be careful not to read outside the buffer


  for(int i=0; i<wlen; i++) {
      dest[i] = (ptr[0] << 8) + ptr[1];
      ptr += 2;
  }
  //cout << "size:: " << size << " wlen:: " << wlen << " c:: " << c << "\n";

  dest[wlen] = ('\0' << 8) + '\0';
  return (wchar_t *)dest;
}

I store this in a value as the property of a struct whilst looping through the file. 我在循环遍历文件时将其存储为值作为结构的属性。

My issue seems to be when I free subbuffer, and start reading the title properties of my structs by looping over an array of struct pointers, my app segfaults. 我的问题似乎是当我释放子缓冲区,并通过遍历结构指针数组(我的应用程序段错误)开始读取结构的标题属性时。 GDB tells me it finished normally though, but a bunch of records that I cout are missing. GDB告诉我它正常完成了,但是我丢失了很多记录。

I suspect this has to do with function scope of something. 我怀疑这与某些功能范围有关。 I thought the memcpy in getStringForSizeAndCursor would fix the segfault since it's copying bytes outside of subbuffer before I free. 我以为getStringForSizeAndCursor中的memcpy可以修复段错误,因为它在释放之前将字节复制到子缓冲区之外。 Right now I would expect those to then be cleaned up by my struct deconstructor, but either things are deconstructing before I expect or some memory is still pointing to the original subbuffer, if I let subbuffer leak I get back the data I expected, but this is not a solution. 现在,我希望这些可以由我的struct deconstructor清除,但是事情在我期望之前就已经在解构,或者某些内存仍指向原始的子缓冲区,如果我让子缓冲区泄漏的话,我会得到我想要的数据,但是不是解决方案。

The only definite error I can see in your question's code is the too small allocation of bcpy, where you allocate a buffer of size size and promptly copy size+2 bytes to the buffer. 我在问题代码中看到的唯一确定的错误是bcpy的分配太小,您在其中分配了大小为size的缓冲区,并立即将size+2个字节复制到该缓冲区中。 Since you're not using the extra 2 bytes in the code, just drop the +2 in the copy. 由于您没有在代码中使用额外的2个字节,因此只需在副本中添加+2。

Besides that, I can only see one suspicious thing, you're doing; 除此之外,我只能看到一件可疑的事情,您在做什么;

char *subbuffer = new char[size+1];

and copying size bytes to the buffer. 并将size字节复制到缓冲区。 The allocation hints that you're allocating extra memory for a zero termination, but either it shouldn't be there at all (no +1) or you should allocate 2 bytes (since your function hints to a double byte character set. Either way, I can't see you zero terminating it, so use of it as a zero terminated string will probably break. 分配提示您正在为零终止分配额外的内存,但要么根本不存在(无+1),要么分配2个字节(因为函数提示为双字节字符集。) ,我看不到您将其零终止,因此将其用作零终止字符串可能会中断。

@Grizzly in the comments has a point too, allocating and handling memory for strings and wstrings is probably something you could "offload" to the STL with good results. 注释中的@Grizzly也有一点,可以为字符串和wstring分配和处理内存,您可以将其“卸载”到STL并获得良好的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM