内存问题(贴代码)~~大家进来帮忙.

xiaoyuezhong 2007-07-10 03:55:55
偶做一个文件的选取与备份.我写了下面这个函数.
问题:
此程序正常编译与运行.
如果没有使用这个选取与备份功能,就能正常关闭.
但是,只要使用了这个功能,就会产生内存错误.(程序运行结果是对的!)
偶找不到原因.我在跟踪代码时发现:在这个执行这个函数后就出现内存错误如下:
'Access Violation at address '00499f0d in module 'project1.exe'. read of address 017c462a',process stopped.use step or run to continue.






void mend(AnsiString subverstr_1 ,AnsiString mendfilepath_1,AnsiString copypath)
{
TSearchRec sr;
int iAttributes = 0;
iAttributes |= faReadOnly;
iAttributes |= faHidden ;
iAttributes |= faSysFile;
iAttributes |= faVolumeID;
iAttributes |= faDirectory;
iAttributes |= faArchive;
iAttributes |= faAnyFile;

if (FindFirst(ExtractFilePath(Application->ExeName) + "\\*.*", iAttributes, sr) == 0)
{
AnsiString c=strcat(subverstr_1.c_str(),"QzServer.exe");
do
{
if (strcmp(sr.Name.c_str(),c.c_str())==0 )
{

AnsiString oldfile_1=strcat(mendfilepath_1.c_str(),sr.Name.c_str());
AnsiString nfile_1=strcat(copypath.c_str(), "QzServer.exe") ;

if ( CopyFile(oldfile_1.c_str(), nfile_1.c_str(),true)==true)
{
// ShowMessage("*****!") ;
}
else
{
// ShowMessage("******!")
}
}

}
while (FindNext(sr) == 0);
FindClose(sr);
}





...全文
74 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyuezhong 2007-07-10
  • 打赏
  • 举报
回复
给分咯..

哈哈.没有想到这么简单.我一直怀疑是strcat 的原因.没想到啊..

谢谢楼上的各位..
xiaoyuezhong 2007-07-10
  • 打赏
  • 举报
回复
谢谢!楼上的!~

:)
cczlp 2007-07-10
  • 打赏
  • 举报
回复
噢,晚了
cczlp 2007-07-10
  • 打赏
  • 举报
回复
strcat 没有这么用的。
可以这样:
AnsiString c=subverstr_1 + QzServer.exe;

另外几个类似
Waiting4you 2007-07-10
  • 打赏
  • 举报
回复
楼主刚从C语言转来吧:)

strcat(subverstr_1.c_str(),"QzServer.exe");
这句不行, c_str()只是返回一个AnsiString的内容指针, 指针本身是不会自动扩容的.

改成subverstr_1 = subverstr_1 + "QzServer.exe";

if(strcmp(sr.Name.c_str(),c.c_str())==0) 改成 if(sr.Name==c)

AnsiString oldfile_1=strcat(mendfilepath_1.c_str(),sr.Name.c_str());
==> AnsiString oldfile_1= mendfilepath_1 + sr.Name;

AnsiString nfile_1=strcat(copypath.c_str(), "QzServer.exe") ;
==> AnsiString nfile_1= copypath + "QzServer.exe";




13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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