简体   繁体   English

C / C ++字符串内存泄漏?

[英]C/C++ string memory leaks?

I'm using the STL string within an application of mine, and I was recently testing it for memory leaks, and I noticed that a lot of my strings weren't being properly deallocated by the end of the program. 我在我的应用程序中使用STL string ,我最近测试它是否存在内存泄漏,我注意到我的很多字符串在程序结束时没有被正确释放。

I tested the following code (not verbatim) with one of the strings: 我用其中一个字符串测试了下面的代码(不是逐字):

const string* cppString = &obj->objString;
const char* cString = cppString->c_str();
delete obj;

After that, I put a break-point and noticed that, while the string that cppString pointed to no longer existed, cString was still pointing to a C-style string, which surely enough, was the one that failed to be deallocated at the end. 在那之后,我提出了一个断点并注意到,虽然cppString指向的string不再存在,但cString仍然指向一个C风格的字符串,这肯定是最后一个未能在最终解除分配的字符串。

Am I missing something in terms of how C/C++ strings work? 我是否遗漏了C / C ++字符串的工作方式? How can I get the C representation of the string to be deallocated as well? 如何才能获取要解除分配的字符串的C表示?

EDIT: Some more information. 编辑:更多信息。 My obj class is of type Dialog , which inherits Popup . 我的obj类是Dialog类型,它继承了Popup I thought that might've been it, since when I delete obj , I'm treating it as a Popup* , but I tried it in a small separate program, and deleting as a parent class properly removes the child member variables (which makes sense, of course). 我认为可能是这样,因为当我删除obj ,我将其视为Popup* ,但我在一个小的单独程序中尝试它,并且作为父类删除正确删除子成员变量(这使得感觉,当然)。

I used the memory leak tracing within VS, and it shows that the string that ended up leaking was the one that was created when I made the Dialog and set the objString to the string passed as a reference to the constructor. 我在VS中使用了内存泄漏跟踪,并且它显示最终泄漏的字符串是在我创建Dialog时创建的字符串,并将objString设置为作为对构造函数的引用传递的字符串。

Thanks, 谢谢,
Jengerer Jengerer

What you're seeing is undefined behavior —it's not actually a memory leak. 您所看到的是未定义的行为 - 实际上并不是内存泄漏。 The memory for the C string has been deallocated (at least as far as you're concerned), but the data there is still technically accessible. C字符串的内存被释放(至少就您而言),但那里的数据仍然可以在技术上访问。 When you deallocate memory, the memory usually doesn't get erased, so the data there often stays around so long as the memory doesn't get reused by a subsequent allocation. 当您释放内存时,内存通常不会被擦除,因此只要内存不会被后续分配重用,数据就会一直存在。

Reading data after it's been deallocated is undefined behavior: you might get what the data was before it was deallocated, you might get garbage data, you might crash your program, or you could even erase your hard drive (although that's not very likely). 在取消分配数据之后读取数据是未定义的行为:您可能会在数据被取消分配之前得到数据,您可能获得垃圾数据,可能会使程序崩溃,或者您甚至可能擦除硬盘驱动器(尽管这不太可能)。

So long as the std::string object is being properly deallocated, then any memory used for its C string representation will also be deallocated. 只要正确释放std::string对象,那么用于其C字符串表示的任何内存也将被释放。 You don't need to worry about that. 你不必担心这一点。


EDIT : Actually it turns out your object wasn't getting fulled destroyed because the parent class Popup didn't have a virtual destructor. 编辑 :事实上,事实证明你的对象没有被破坏,因为父类Popup没有虚拟析构函数。 As a result, the destructor for the subclass Dialog wasn't getting called, so the destructor for the std::string instance wasn't getting called. 结果,没有调用子类Dialog的析构函数,因此没有调用std::string实例的析构函数。

The problem is most likely not in std::string , but in obj (whatever type that is). 问题很可能不是在std::string ,而是在obj (无论是什么类型)。 Note that you deleted obj , not cppString . 请注意,您删除了obj ,而不是cppString My guess is that obj does not store objString in a smart pointer class nor does it delete objString in its destructor, and hence you have that leak. 我的猜测是obj不会在智能指针类中存储objString ,也不会在析构函数中删除objString ,因此你就有了这个漏洞。

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

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