Why is it necessary to remove the IAviFile pointer before calling AviFileExit ()?

I found a post with a sample showing how to get the duration of an AVI file:

Getting the duration of an AVI file

I modified it for my purposes in my Delphi 6 application and generated the code below. I originally deleted the line that hovered the IAviFile pointer before calling AviFileExit (). But when I did this, I got an access violation when AviFileExit () is called. I restored the string and violated access rights.

Why is it necessary to name the IAviFile link before calling AviFileExit ()? Is it a memory leak? I would think that a normal count of links to an interface would work correctly here, but obviously this is not the case. Is there any other way to fix the error, for example, call AviStreamRelease () or the like?

Here is my code:

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;
+3
1

, Delphi , AVIFileRelease() . AVIFileRelease() nil, - , . , Delphi Release() , ( AVIFileExit()) .

IAVIFile IUknown, , Microsoft AVIFileRelease() . , . , . Microsoft .

+5

All Articles