Search code examples
c++new-operatorcode-analysismicrosoft-code-analysis

C++ compiler waring C26409 - avoid calling new and delete


Here is my code that I'm getting (Warning C26409 Avoid calling new and delete explicitly, use std::make_unique instead (r.11).) after doing a Visual Studio 2019 Code Analysis:

#include <windows.h>
#include <strsafe.h>
int main()
{
    auto *sResult = new WCHAR[256];
    StringCchPrintfW(sResult, 256, L"this is a %s", L"test");
    delete[] sResult;
}

I was under the assumption to use new/delete instead of calloc/free, but now the compiler is telling me to use std::make_unique. I can't find any examples on how I can change my code to be compliant.

So my questions are:

  1. how do I change my code so it doens't use new/delete

  2. why should I not use new/delte vs std::make_unique?


Solution

  • Here's how you could rewrite this using std::unique_ptr. Note that the use of std::vector<WCHAR>, std::wstring or an array allocated on the stack would be preferable:

    #include <memory>
    
    ...
    
    {
        auto sResult = std::make_unique<WCHAR[]>(256);
        StringCchPrintfW(sResult.get(), 256, L"this is a %s", L"test");
    } // exiting the scope frees the memory
    

    Why should you do this (or use std::vector or similar?

    It simplifies the code, since you don't need to think about how the function is exited; the compiler ensures the resources are freed. Otherwise you need to make sure for every execution path including ones that throw an exception or return early free all resources. The programming technique is called RAII (Resource Allocation Is Initialization) in C++.


    Note: Given the small amount of memory you allocate, you may want to create the memory on the stack though. This should also get rid of the error by removing any dynamic memory allocations:

    WCHAR sResult[256];
    StringCchPrintfW(sResult, 256, L"this is a %s", L"test");