是不是dephi 6的bug!(40分)

  • 主题发起人 主题发起人 lmh79
  • 开始时间 开始时间
L

lmh79

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; Name:pchar;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //读取ini文件<br>&nbsp; &nbsp; &nbsp; &nbsp; Name:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; GetPrivateProfileString(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Tab pages',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Forms',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; './Tab.ini'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br><br>end;<br>end.<br>在Dephi 6中出现如下错误,请高手指点:<br>Project Project1.exe raised exeception class EAccessViolation with message 'Access<br>Violation at address 77E70782 in module 'kernel32.dll'. Write of address 00451620'.<br>Process stopped.Use Step or Run to continue.
 
可以确定不是!我用过。换一下delphi6试试。
 
我认为是你的Name变量的类型设置有问题,PChar和 array of Char 在很多情况下不能混淆;<br>尤其是API函数的参数中;<br>下面程序在D6中可运行<br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; Name:array[0..99] of Char; //请注意此处改动!<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //读取ini文件<br>&nbsp; &nbsp; &nbsp; &nbsp; Name:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; GetPrivateProfileString(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Tab pages',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Forms',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C:/Tab.ini'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br>end;
 
Why not use the Tinifile;
 
同意Bill_Liyi
 
PChar 是个指向字符串的指针,你在声明后没有分配空间就使用,当然回出错了。错误就是标准的<br>GP(通用保护错):内存地址越界。为此,如果你要用PChar必须注意它的地址空间分配<br>问题。<br>PChar在栈(Stack)中分配一个地址指针<br>array[0..99] of Char在栈(Stack)中分配连续的100个字符的空间,<br>String在堆(Heap)中自动动态分配长度(空间),支持自动垃圾回收操作。
 
用getmem(name,100)分配空间<br>...<br>...<br>freemem(name) 释放空间
 
同意楼上的
 
同意上面两位,PChar只是个指针,在没初始化(分配内存)就使用当然要出错<br>不能跟String一样使用,String其实是个自动字符串数组,分配、释放、维护由Delphi自动完成。<br>而Pchar只是个指针
 
后退
顶部