无效的指针操作
我有一个包含 TOpenDialog
组件 (OpenDialog1
) 和一个按钮的表单。 OpenDialog1
将 ofAllowMultiSelect
(Options
)属性设置为 true。
单击按钮后,将执行方法 AddFilesToListView
:
procedure TForm4.AddFilesToListView();
var
ListItem : TListItem;
I: Integer;
F : File;
LengthOfAudio : TDateTime;
previousCursor : TCursor;
begin
previousCursor := Self.Cursor;
Self.Cursor := crHourGlass;
if OpenDialog1.Execute then
begin
for I := 0 to OpenDialog1.Files.Count - 1 do begin
if FileExists(OpenDialog1.FileName) then begin
ListItem:=ListView1.Items.Add;
ListItem.Caption := 'Test';
ListItem.SubItems.Add(ExtractFileName(OpenDialog1.Files[I]));
ListItem.SubItems.Add(ExtractFilePath(OpenDialog1.Files[I]));
end else
raise Exception.Create('File does not exist.');
end;
end;
Self.Cursor := previousCursor;
OpenDialog1.Files.Free;
end;
运行应用程序时,选择第一个文件,我没有问题,但当想要选择第二个文件时,我收到一条错误消息“Project project3 raise an带有消息“无效指针操作”的异常类 EInvalidPointer。”
这是什么原因,我该如何纠正?
I have a form that contains a TOpenDialog
component (OpenDialog1
) and a button.OpenDialog1
has the ofAllowMultiSelect
(of Options
) property set to true.
Upon clicking the button the method AddFilesToListView
is executed:
procedure TForm4.AddFilesToListView();
var
ListItem : TListItem;
I: Integer;
F : File;
LengthOfAudio : TDateTime;
previousCursor : TCursor;
begin
previousCursor := Self.Cursor;
Self.Cursor := crHourGlass;
if OpenDialog1.Execute then
begin
for I := 0 to OpenDialog1.Files.Count - 1 do begin
if FileExists(OpenDialog1.FileName) then begin
ListItem:=ListView1.Items.Add;
ListItem.Caption := 'Test';
ListItem.SubItems.Add(ExtractFileName(OpenDialog1.Files[I]));
ListItem.SubItems.Add(ExtractFilePath(OpenDialog1.Files[I]));
end else
raise Exception.Create('File does not exist.');
end;
end;
Self.Cursor := previousCursor;
OpenDialog1.Files.Free;
end;
When running the application, selecting the first file, I have no problem but when wanting to select the second one, I get an error saying "Project project3 raised an exception class EInvalidPointer with message 'Invalid Pointer Operation'."
What's the cause of this, how do I correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“无效的指针操作”意味着你释放了不属于你的内存。 这三件事之一就是原因:
在您的代码中,您将释放
TOpenDialog
的Files
属性。 您没有分配该字符串列表,并且文档没有告诉您释放它,因此可以合理地预期该列表实际上属于对话框组件,并且该组件将在需要时释放它。 检查 Dialogs.pas 中的源代码证实了这一点。 由于您还释放了该对象,因此出现了双重释放错误,这符合我上面列出的第一个标准。 删除该行。正如 Uwe 指出的那样,您还在处理一个列表> 文件名,但仅检查一个文件名是否存在。 这是您的程序中的逻辑错误,但它不会导致您看到的异常。
"Invalid pointer operation" means you freed memory that didn't belong to you. One of these three things is the cause:
In your code, you're freeing the
TOpenDialog
'sFiles
property. You didn't allocate that string list, and the documentation doesn't tell you to free it, so it's reasonable to expect that the list actually belongs to the dialog component, and that the component will free it when it wants. Checking the source code in Dialogs.pas confirms that. Since you have also freed that object, you have a double-free error, which meets the first criterion I listed above. Remove that line.As Uwe pointed out, you're also processing a list of file names but only checking the existence of one. That's a logic error in your program, but it would not cause the exception you're seeing.
您应该检查
而不是
更好地投资保存该值的局部变量。
为什么会这样呢?
You should check for
instead of
Better invest in a local variable holding that value.
And why this?
文件由 TOpenDialog 所有,不应直接释放。
Files is owned by the the TOpenDialog, and should not be freed directly.