可能是堆被损坏,这也说明 XX.exe 中或它所加载的任何 DLL 中有 bug

在C++编程中遇到一个运行错误,当使用cout输出小数时,无论选择“多线程DLL(MD)”还是“多线程调试DLL(MDd)”运行库,都会引发未经处理的异常。问题可能源于堆损坏,暗示代码或其加载的DLL存在bug。通过逐步调试,发现strcpy函数可能导致崩溃,但删除该函数后,虽然避免了崩溃,却产生了内存泄漏问题。最终确定问题是由越界访问导致,这影响了free和delete操作,引发错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天遇到一个很奇怪的问题,

当代码如下时:

char* s = (char*)malloc(20*sizeof(char));
string buffer;
const char* conchar;
char* p;
double x = 0.01;
while(getline(file,buffer)){
	const int len = buffer.length();
	conchar = buffer.c_str();//m_conchar为const char *
	strcpy(s,conchar);//换成char*

	cout<<x<<endl;
}

上述不是我完整的程序,cout是实例,就是每次运行到cout只要输出是小数都会出错,而int类型啥的都不出错。

当工程设置运行库“多线程DLL(MD)”

XXX.exe 中的 0x7707e4e4 (ntdll.dll) 处有未经处理的异常: 0xC0000005: 写入位置 0x0000000000000024 时发生访问冲突

当工程设置运行库“多线程调试DLL(MDd)”

只要是cout或者file.close()或者free(s)都会出现

其原因可能是堆被损坏,这也说明 XX.exe 中或它所加载的任何 DLL 中有 bug

http://blog.youkuaiyun.com/blz_wowar/article/details/2176536

但是感觉这些都不适用,没有自己加载的dll。

之后一点一点注释,发现没有strcpy这个函数就不会崩溃,而加上之后在cout处还是会崩溃,每次出错都在C的运行时库,后来把代码改成了

//char* s= (char*)malloc(20*sizeof(char));
string buffer;
//const char* conchar;
char* p;
double x = 0.01;
while(getline(file,buffer)){
	const int len = buffer.length();
	char* s = new char[len];
	strcpy(s,buffer.c_str());
	//conchar = buffer.c_str();//m_conchar为const char *
	//strcpy(s,conchar);//换成char*
	cout<<x<<endl;
	//delete s;
}

这样可以顺利运行,但是delete的时候程序会有问题,这样就造成了内存泄露,很奇怪。

 

最后搞清楚了这种情况是越界了,所以free会出错,delete也会出错,http://blog.youkuaiyun.com/kuaile123/article/details/10215483

# # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000002198f97ba40, pid=17124, tid=18936 # # JRE version: OpenJDK Runtime Environment JBR-21.0.5+8-631.30-jcef (21.0.5+8) (build 21.0.5+8-b631.30) # Java VM: OpenJDK 64-Bit Server VM JBR-21.0.5+8-631.30-jcef (21.0.5+8-b631.30, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64) # Problematic frame: # j io.netty.channel.nio.NioIoHandler.select(Lio/netty/channel/IoExecutionContext;Z)V+99 # # No core dump will be written. Minidumps are not enabled by default on client versions of Windows # # If you would like to submit a bug report, please visit: # https://youtrack.jetbrains.com/issues/JBR # --------------- S U M M A R Y ------------ Command Line: abort vfprintf -XX:ErrorFile=C:\Users\27663\java_error_in_pycharm_%p.log -XX:HeapDumpPath=C:\Users\27663\java_error_in_pycharm.hprof -Xms256m -Xmx2048m -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -XX:CICompilerCount=2 -XX:+IgnoreUnrecognizedVMOptions -ea -Dsun.io.useCanonCaches=false -Dsun.java2d.metal=true -Djbr.catch.SIGABRT=true -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Djdk.nio.maxCachedBufferSize=2097152 -Djava.util.zip.use.nio.for.zip.file.access=true -Dkotlinx.coroutines.debug=off -XX:+UnlockDiagnosticVMOptions -XX:TieredOldPercentage=100000 -Dwelcome.screen.defaultWidth=1000 -Dwelcome.screen.defaultHeight=720 -Dwsl.use.remote.agent.for.nio.filesystem=true -Djava.nio.file.spi.DefaultFileSystemProvider=com.intellij.platform.core.nio.fs.MultiRoutingFileSystemProvider -Djava.security.manager=com.intellij.platform.core.nio.fs.CoreBootstrapSecurityManager --add-opens=java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED --add-opens=java.base/jdk.internal.org.objectweb.asm.tree=ALL-UNNAMED -javaagent:C:\Users\27663\AppData\Roaming\PyCharm\
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值