C4150 deletion of pointer to incomplete type ''; no destructor called

I tried delete my structure
 
  delete it;

and get warning, what this type is incomplete.
Last edited on
At the point at which delete is invoked, the struct has been declared, but not defined.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct A ; // declare A: A is an incomplete type at this point

void foo( A* ptr ) // ptr is a pointer to an incomplete type at this point
{
    // LLVM clang++: *** warning: deleting pointer to incomplete type 'A' may cause undefined behavior
    //                   note: forward declaration of 'A' (line 3)

    // GNU g++     :     *** warning: possible problem detected in invocation of delete operator
    //                   *** warning: 'ptr' has incomplete type
    //                   note: forward declaration of 'struct A' (line 3)
    //                   note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined

    // Microsoft cl: *** warning: deletion of pointer to incomplete type 'A'; no destructor called
    //                   note: see declaration of 'A' (line 3)
    delete ptr ; 
}

struct A { /* ... */ }; // define A: A is a complete type at this point

void bar( A* ptr ) // // ptr is a pointer to a complete type at this point
{
    delete ptr ; // this is fine
}

http://coliru.stacked-crooked.com/a/d877d55798b9cf8e
http://rextester.com/GON79298

Define the struct before the line that caused the warning.
(If the definition is in a header, #include the header)
I can't use full declaration in header while including header with forward declaration not work.
At the moment, I have found solution: create function for delete in same file with structure and just call its from other file. Its should work.
Last edited on
I'am right? It's will be work correctly?
Yes.
Registered users can post here. Sign in or register to post.