Skip to content

Placement new and std::nothrow do not work with MemoryLeakDetectorNewMacros.h #1565

@djboni

Description

@djboni

Hello, how are you?

I think I have found an interesting bug.

I am trying to use the placement new to construct in a previously allocated buffer.

However the macro #define new new(__FILE__, __LINE__), defined in MemoryLeakDetectorNewMacros.h, seems to break the placement new.

/* file.cpp */
void f()
{
    // Buffer
    unsigned char buff[100];

    // Placement new
    new (buff) int(0);
}

Compiling the code above gives the following error when including MemoryLeakDetectorNewMacros.h.

src/file.cpp:7:10: error: ‘buff’ does not name a type
    4 |     new (buff) int(0);
      |          ^~~~

There is a workaround, which consists of #undef the macro before the placement new.

/* file.cpp */
void f()
{
    // Buffer
    unsigned char buff[100];

    // Workaround
#ifdef new
    #undef new
#endif

    // Placement new
    new (buff) int(0);
}

I hope that we can fix this, since the placement new is important on embedded systems.

If there are any questions, just let me know.

Best regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions