简体   繁体   中英

Am I deleting this pointer correctly?

I have the following code:

LPWSTR pszDSPath = NULL;
pszDSPath = new WCHAR[  wcslen(pwszFilter)+
                        wcslen(wstrServer.c_str())+
                        wcslen(var.bstrVal) +
                        1
                     ];

// ....
// ....

if(pszDSPath)
{
    delete pszDSPath; 
    pszDSPath = NULL;
}

Can the above code generate a memory leak? I'm not sure if I'm deleting pszDSPath correctly or not.

You are not using the correct delete . There are two forms of new : the scalar new that creates a single object (eg new int ), and the array new that creates an array (eg new int[42] ).

Likewise, there are two forms of delete : delete and delete[] . If you use new , you must use delete to destroy the object and if you use new[] you must use delete[] to destroy the object.

Since you have used new[] to create the object pointed to by pszDSPath , you must use delete[] pszDSPath to destroy the object.

Note, however, that this would be made much easier if you just used a std::vector :

std::size_t n = wcslen(pwszFilter)+
                wcslen(wstrServer.c_str())+
                wcslen(var.bstrVal) +
                1;

std::vector<WCHAR> v(n);

// &v[0] can be used as you are using pszDSPath in your current code.

In C++, you should eschew manual memory management: it is extraordinarily difficult to get right and it takes a lot of work. There are library facilities in the C++ Standard Library, including containers like std::vector and std::map and smart pointers like std::auto_ptr , std::unique_ptr , and std::shared_ptr , that manage object lifetimes for you. You shouldn't do more work than you have to: if you think you have to write delete somewhere in your code, your code is probably wrong.

This principle of using containers to manage resource lifetimes is based on a design pattern called Scope-Bound Resource Management (SBRM) or Resource Acquisition is Initialization (RAII) .

( std::unique_ptr is a part of C++0x designed to replace std::auto_ptr , and your compiler may not yet support it. std::shared_ptr is also a part of C++0x, but it has been available for about a decade as a part of the Boost libraries (as boost::shared_ptr ) and was included in C++ TR1.)

预先分配数组时,请使用delete[] pszDSPath避免内存泄漏。

Use delete[] to avoid undefined behavior.

However using delete in you case will almost never cause a memory leak alone - either it will just work or you'll get something much much worse than a memory leak. Don't risk - use delete[] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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