How serious is the new/delete operator mismatch error?
It's undefined behavior serious (it could work, it could crash, it could do something else).
Getting new-delete-type-mismatch from ASAN
C++ repeatedly gives you that feeling that you still do not understand even fundamental concepts. In this case: Inheritance.
By adding print statements to the ctors and dtors, you will find that for both pointers (old style and smart pointer) only ~A, not ~B is called. This is because A's dtor is not virtual.
Scott Meyers says: "Polymorphic base classes should declare virtual destructors. If a class has any virtual functions, it should have a virtual destructor"
Fix this by adding
struct A {
virtual ~A() = default;
};
Memory new-delete-type-mismatch when using std::shared_ptr
When use std::priority_queue::top()
it doesn't check if size() > 0
, so std::priority_queue::top()
use dealocated memory to use shared_ptr
methods on, so cause bad things
Deleting an array created by new [] with delete []
The behaviour on using delete
on a pointer acquired by new[]
is undefined. (Some C++ runtime libraries are implemented in such a way that an array of plain old data types is delete
d correctly when you write delete
).
Really there is nothing else to say.
How to correctly delete a pointer after using new operator? Code Keeps Throwing Error
The short answer is, remove all pointers, new and delete from your code.
There is no need for it anywhere.
Literally remove every call to both, and remove every *
(except when being used to multiply), and your code becomes correct.
Not for this problem, but in other situations, you need to read up on the rule of five, and use unique_ptr
by default, and call make_unique
instead of new
directly.
Specific problems causing your crash include calling new
, then immediately assigning over the pointer, losing track of that new pointer. Then you double delete.
But as noted, nothing in your code shown should be using pointers.
Is this possible that new without bracket but delete with bracket in C++
No, it's not ok. Behaviour will be at best undefined.
edit:
Info link added by popular request: http://web.archive.org/web/20080703153358/http://taossa.com/index.php/2007/01/03/attacking-delete-and-delete-in-c
new / delete mismatch
In Visual Studio 2013 (and older), the default delete[]
operator is not built in the binary of the application (as opposed to the default new
, new[]
and delete
operators). It is called directly from msvcr110d.dll.
When the inline keyword is used, the replaced new[]
and delete[]
operators are not used as default in the whole application (as opposed to the new
and delete
operators) unless we explicitly include the header.
So the default new[]
operator (built in the binary of the application) calls the replaced new
operator.
But the default delete[]
operator (called directly from msvcr110d.dll) doesn't find the replaced delete
operator and use the default delete
operator (from msvcr110d.dll).
For my solution, I removed the inline keyword and put the operators in a source file.
This bug has been fixed and should not appear in Visual Studio 2015 (and newer).
Should I delete variables if created them using new operator inside map::insert()?
So..
As t.niese answered 0xC0000005 exit status in my code came from a difference between creating and deleting methods used.
When using new, you should delete it using delete
When using new[], you should use delete[]
Related Topics
Both Asterisk and Ampersand in a Parameter
What Does the Gcc Warning "Project Parameter Passing for X Changed in Gcc 7.1" Mean
Read from Qtcpsocket Using Qdatastream
How to Enforce C++ Compiler to Use Specific Crt Version
Conversion from Derived** to Base**
Visual C++ "For Each" Portability
Calculating the Info-Hash of a Torrent File
Why Is a Default Constructor Required When Storing in a Map
What Is the Reason Behind Cbegin/Cend
Class Members and Explicit Stack/Heap Allocation
Addition of Two Pointers in C or C++ Not Supported. Why
Variable or Field Declared Void
Efficiently Convert Between Hex, Binary, and Decimal in C/C++
How to Create a Std::Set of Structures
Is There a Clean Way to Prevent Windows.H from Creating a Near & Far MACro
Calling Base Class Definition of Virtual Member Function with Function Pointer