无法删除指针,已触发断点
Cant delete pointer, has triggered a breakpoint
我在在线课程中学习了 C++,但在 Visual Studio 中遇到 "delete" 语句的问题。执行删除时,它打开一个文件 delete_scalar.cpp
并显示:
ConsoleApplication.exe has triggered a breakpoint. exception thrown
我已经从 temp 移动了 SymbolCache 文件夹,包括 wntdll.pdb 等
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int *pInt = new int;
*pInt = 3;
cout << *pInt << endl;
cout << pInt << endl;
delete[] pInt; //error -- UPDATED: still get error with `delete pInt;`
return 0;
}
这里是输出信息,
'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Symbols loaded.
'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Symbols loaded.
'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Symbols loaded.
The thread 0x2fd0 has exited with code 0 (0x0).
HEAP[ConsoleApplication11.exe]: Invalid address specified to RtlValidateHeap( 00930000, 009359E8 )
ConsoleApplication11.exe has triggered a breakpoint.
delete_scalar.cpp 从视觉本身,
//
// delete_scalar.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>
_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK); // the circle X symbol showed up there
#else
free(block);
#endif
}
我已经做了启用和删除等断点操作,但断点仍然在 delete_scalar.cpp 上触发,并出现圆圈 X 符号。
如果用new
请求内存,需要用delete
删除。
在使用 new [ ]
时仅使用 delete [ ]
。两者不匹配会导致未定义的行为。
我在在线课程中学习了 C++,但在 Visual Studio 中遇到 "delete" 语句的问题。执行删除时,它打开一个文件 delete_scalar.cpp
并显示:
ConsoleApplication.exe has triggered a breakpoint. exception thrown
我已经从 temp 移动了 SymbolCache 文件夹,包括 wntdll.pdb 等
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int *pInt = new int;
*pInt = 3;
cout << *pInt << endl;
cout << pInt << endl;
delete[] pInt; //error -- UPDATED: still get error with `delete pInt;`
return 0;
}
这里是输出信息,
'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Symbols loaded. The thread 0x2fd0 has exited with code 0 (0x0). HEAP[ConsoleApplication11.exe]: Invalid address specified to RtlValidateHeap( 00930000, 009359E8 ) ConsoleApplication11.exe has triggered a breakpoint.
delete_scalar.cpp 从视觉本身,
//
// delete_scalar.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>
_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK); // the circle X symbol showed up there
#else
free(block);
#endif
}
我已经做了启用和删除等断点操作,但断点仍然在 delete_scalar.cpp 上触发,并出现圆圈 X 符号。
如果用new
请求内存,需要用delete
删除。
在使用 new [ ]
时仅使用 delete [ ]
。两者不匹配会导致未定义的行为。