开发者

Am I deleting this pointer correctly?

开发者 https://www.devze.com 2023-02-11 19:30 出处:网络
I have the following code: LPWSTR pszDSPath = NULL; pszDSPath = new WCHAR[wcslen(pwszFilter)+ wcslen(wstrServer.c_str())+

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; 
    pszDSP开发者_StackOverflow社区ath = 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 (e.g. new int), and the array new that creates an array (e.g. 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.)


Use delete[] pszDSPath to avoid memory leaks when you have allocated an array beforehand.


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[].

0

精彩评论

暂无评论...
验证码 换一张
取 消