我发现了一个Stack Overflow的帖子,其中包含一个示例,展示如何获取AVI文件的持续时间:
Getting AVI file duration
我在我的Delphi 6应用程序中修改了它,并创建了以下代码。最初,我删除了在调用AviFileExit()之前清除IAviFile指针的行。但是,当我这样做时,调用AviFileExit()时会出现访问冲突。我恢复了该行,访问违规消失了。
为什么在调用AviFileExit()之前清除IAviFile引用是必要的?这是内存泄漏吗?我认为正常的接口引用计数在这里应该可以正常工作,但显然不是这样。是否有另一种方法来消除错误,比如调用AviStreamRelease()或类似的函数?
以下是我的代码:
为什么在调用AviFileExit()之前清除IAviFile引用是必要的?这是内存泄漏吗?我认为正常的接口引用计数在这里应该可以正常工作,但显然不是这样。是否有另一种方法来消除错误,比如调用AviStreamRelease()或类似的函数?
以下是我的代码:
function getAviDurationSecs(theAviFilename: string): Extended;
var
aviFileInfo : TAVIFILEINFOW;
intfAviFile : IAVIFILE;
framesPerSecond : Extended;
begin
intfAviFile := nil;
AVIFileInit;
try
// Open the AVI file.
if AVIFileOpen(intfAviFile, PChar(theAviFilename), OF_READ, nil) <> AVIERR_OK then
raise Exception.Create('(getAviDurationSecs) Error opening the AVI file: ' + theAviFilename);
try
// Get the AVI file information.
if AVIFileInfoW(intfAviFile, aviFileInfo, sizeof(aviFileInfo)) <> AVIERR_OK then
raise Exception.Create('(getAviDurationSecs) Unable to get file information record from the AVI file: ' + theAviFilename);
// Zero divide protection.
if aviFileInfo.dwScale < 1 then
raise Exception.Create('(getAviDurationSecs) Invalid dwScale value found in the AVI file information record: ' + theAviFilename);
// Calculate the frames per second.
framesPerSecond := aviFileInfo.dwRate / aviFileInfo.dwScale;
Result := aviFileInfo.dwLength / framesPerSecond;
finally
AVIFileRelease(intfAviFile);
// Commenting out the line below that nukes the IAviFile
// interface reference leads to an access violation when
// AVIFileExit() is called.
Pointer(intfAviFile) := nil;
end;
finally
AVIFileExit;
end;
end;