VC2005程序在Ctrl+F5下出错,但是在F5下不会出错,会是啥问题呢??

rocket_11 2009-03-29 01:03:36
VC2005程序在Ctrl+F5下出错,但是在F5下不会出错.
感觉很茫然的错误,VC用的不多,程序除了用dll,都是用标准c,c++
还有,能保证没有跨dll内存分配问题

跟踪了一下,在malloc的时候出错了。
我的程序是按照页面来分配内存的
如下:

inline PageLayoutPtr allocZeroPageLayout()
{
fprintf(stderr,"before malloc!\n");
void* ptr = std::malloc(PAGE_SIZE);
fprintf(stderr, ptr == NULL ? "malloc error!\n" : "malloc success!\n");
std::memset(ptr, 0, PAGE_SIZE); // XXX 考虑按字长的memset
fprintf(stderr, "memset sucess!\n");
return static_cast<PageLayoutPtr>(ptr);
}

其中页面大小 PAGE_SIZE = 4096
空闲的内存自己管理。

现在怀疑是不是自己管理导致越界,然后修改了malloc用来管理分配出的内存的部分导致malloc调用的时候直接崩溃。

可是,为什么在F5下就不会出错呢??

============== 等待大家的帮助啦 ;-) ===============
...全文
389 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
genius_tong 2009-03-29
  • 打赏
  • 举报
回复
会有这种事??
rocket_11 2009-03-29
  • 打赏
  • 举报
回复
结贴,把程序用g++编译了,在linux跑,同学帮忙用valgrind跟了半天,发现了,自己在管理内存的时候犯了一个错误,
可能导致在一个已页面的尾部,多写16字节以内的数据,然后破坏了malloc的堆池(??)

不过的确,debug下的Ctrl+F5和F5的内存管理不同,
> When you run your code in the debugger, it puts guard bytes around your
> memory allocations.

这段不是说debug和release的区别,而是指程序是否由debugger启动,
如果debugger启动的话,有guard bytes的保护,导致这16个字节以内的越位很难察觉出来,但是,不从debugger启动的话,没有guard bytes的保护,就会直接失败

失败的点很诡异,在局部变量str中,应该是str内部也是在堆中分配内存,所以内存冲突导致错误。

谢谢大家的意见,这次的教训是:
(1)C中的内存不要乱用,最好有一个点完全管理内存的点;
(2)学习使用自动化工具查bug
fairchild811 2009-03-29
  • 打赏
  • 举报
回复
应该不至于吧
lingyin55 2009-03-29
  • 打赏
  • 举报
回复
Ctrl+F5出错的话在F5应该也会出错的,

[Quote=引用楼主 rocket_11 的帖子:]
VC2005程序在Ctrl+F5下出错,但是在F5下不会出错.
感觉很茫然的错误,VC用的不多,程序除了用dll,都是用标准c,c++
还有,能保证没有跨dll内存分配问题

跟踪了一下,在malloc的时候出错了。
我的程序是按照页面来分配内存的
如下:

C/C++ code
inline PageLayoutPtr allocZeroPageLayout()
{
fprintf(stderr,"before malloc!\n");
void* ptr = std::malloc(PAGE_SIZE);

[/Quote]
sagegz 2009-03-29
  • 打赏
  • 举报
回复
如果出问题,2个都应该出问题的.不相信LZ随便搞个内存越界的试试.
mabo321 2009-03-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rocket_11 的回复:]
> When you run your code in the debugger, it puts guard bytes around your
> memory allocations. It's a dubious feature, because it doesn't check by
> default, so you can easily read and write to the guard bytes in the
> debugger, and nothing happens, but in the release build, this could be fatal
============== google上搜的 ===================

感觉应该是有内存越界了,再检查下代码 :-( …
感觉上,Ctrl + F5和F5运行的时候,malloc的内存分配策略不同。
[/Quote]

这是 debug 运行 和 release 运行 的区别吧,
像assert()只能用在debug 运行中……

也许象你说的:Ctrl + F5和F5运行的时候,malloc的内存分配策略
但是,这种不同 不可能 到让程序 崩溃的地步 吧……
alan001 2009-03-29
  • 打赏
  • 举报
回复
应该不出问题
rocket_11 2009-03-29
  • 打赏
  • 举报
回复
我机子上没有装VC6,在同学的电脑下用VC编了一个静态连接的,没有问题,Ctrl+F5或者直接运行exe,或者F5都没问题;

=================================================================================
在自己机子上运行同学编译的exe, 正常;
在自己机子上用VC2005静态编译, Ctrl+F5崩溃,F5正常,直接运行exe崩溃;
在自己机子上用VC2005编译测试工程,依赖dll工程,Ctrl+F5崩溃,F5正常,直接运行exe崩溃;
在自己机子上用MinGW 静态编译, Ctrl+F5崩溃,F5正常,直接运行exe崩溃;
=================================================================================
我的VC2005没有打过补丁,会是这个问题么,但是MinGW为啥出错呢,出错的点和VC2005不一样;


int main() {

printf("size of index_hdr = %d\n", sizeof(cfg::index_hdr));

cfg::Cfg* pCfg = cfg::Cfg::createCfg(cfg::Cfg::TAG_SIMPLE);

const unsigned int scope = 4096;
const unsigned int testnum = 10000;
srand(0);
std::string* szstrs[testnum];
szstrs[0] = new std::string(10, 'a');

for (int i = 1; i < testnum; i++)
{
fprintf(stderr, "(((((((((((((((( Test %d ))))))))))))))\n", i);
fprintf(stderr, "before rand()\n");
unsigned int size = (unsigned int)rand() % scope;
//unsigned int size = 5;
fprintf(stderr, "after rand()\n");
fprintf(stderr, "size = %u\n", size);
std::string str(size, 'b');
// VC2005尽然在上行崩掉了,换成堆就没出错,我修改栈大小为10M reserve, 1M commit还出错
fprintf(stderr, "after string\n");
fprintf(stderr, "before putData()\n");
pCfg->putData(i, str.c_str(), size);
fprintf(stderr, "after putData()\n");
}

return 0;
}
mengde007 2009-03-29
  • 打赏
  • 举报
回复
我这没问题……
T技术沙龙 2009-03-29
  • 打赏
  • 举报
回复
看看你的快捷方式指向什么功能
老邓 2009-03-29
  • 打赏
  • 举报
回复
是否与程序目录有关?
rocket_11 2009-03-29
  • 打赏
  • 举报
回复
> When you run your code in the debugger, it puts guard bytes around your
> memory allocations. It's a dubious feature, because it doesn't check by
> default, so you can easily read and write to the guard bytes in the
> debugger, and nothing happens, but in the release build, this could be fatal
============== google上搜的 ===================

感觉应该是有内存越界了,再检查下代码 :-(
感觉上,Ctrl + F5和F5运行的时候,malloc的内存分配策略不同。
rocket_11 2009-03-29
  • 打赏
  • 举报
回复
打印信息确定在

void* ptr = std::malloc(PAGE_SIZE);

这一行崩溃了,如何stderr打印看呢,请详细指教 ;-), 谢谢啦
  • 打赏
  • 举报
回复
stderr里的信息打出来看看就知道了。
rocket_11 2009-03-29
  • 打赏
  • 举报
回复
恩,调试运行下没有问题,但是直接运行就崩溃 ...
hylove9494 2009-03-29
  • 打赏
  • 举报
回复
F5是调试运行吧,vs2005出bug了?不会啊,用的很好啊!

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧