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

Dec 24, 2017 at 6:57am
I tried delete my structure
 
  delete it;

and get warning, what this type is incomplete.
Last edited on Dec 24, 2017 at 9:25am
Dec 24, 2017 at 10:46am
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)
Dec 24, 2017 at 11:20am
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 Dec 24, 2017 at 11:28am
Dec 25, 2017 at 1:15pm
I'am right? It's will be work correctly?
Dec 25, 2017 at 1:16pm
Yes.
Topic archived. No new replies allowed.