Skip to content

Instantly share code, notes, and snippets.

@azat
Created September 10, 2022 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azat/027f0e949ea836fc2e6269113ceb8752 to your computer and use it in GitHub Desktop.
Save azat/027f0e949ea836fc2e6269113ceb8752 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <memory>
struct NewDeleteOperators
{
NewDeleteOperators() = default;
static void* operator new(size_t size)
{
return ::malloc(size);
}
static void operator delete(void *p) noexcept
{
::free(p);
}
};
void testNewDeleteOperators() {
std::unique_ptr<NewDeleteOperators> op;
op.reset(new NewDeleteOperators);
}
@azat
Copy link
Author

azat commented Sep 10, 2022

$ clang-tidy test-newdelete-tidy.cpp
/tmp/test-newdelete-tidy.cpp:20:1: warning: Potential leak of memory pointed to by 'op._M_t._M_t._M_head_impl' [clang-analyzer-unix.Malloc]
}
^
/tmp/test-newdelete-tidy.cpp:19:12: note: Calling 'NewDeleteOperators::operator new'
  op.reset(new NewDeleteOperators);
           ^~~~~~~~~~~~~~~~~~~~~~
/tmp/test-newdelete-tidy.cpp:10:12: note: Memory is allocated
    return ::malloc(size);
           ^~~~~~~~~~~~~~
/tmp/test-newdelete-tidy.cpp:19:12: note: Returning from 'NewDeleteOperators::operator new'
  op.reset(new NewDeleteOperators);
           ^~~~~~~~~~~~~~~~~~~~~~
/tmp/test-newdelete-tidy.cpp:20:1: note: Potential leak of memory pointed to by 'op._M_t._M_t._M_head_impl'
}
^
$ clang --version
clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment