首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ppPins过滤器的EnumPins方法(Delphi/DSPACK)中处理空EnumPins参数的正确方法?

在ppPins过滤器的EnumPins方法(Delphi/DSPACK)中处理空EnumPins参数的正确方法?
EN

Stack Overflow用户
提问于 2011-10-28 23:29:32
回答 2查看 775关注 0票数 2

我有一个使用Delphi 6编写的自定义推送源代码过滤器,它使用Windows /32机器上的DSPACK DirectShow组件库。在编码过程中,我在打开应用程序编译器选项中的范围检查之后,在属于DSPACK (BaseClass.pas)的BaseClass单元中遇到了一个与BaseClass单元中发生的范围检查错误有关的问题。

错误发生在我的EnumPins()方法中。注意,此方法驻留在DSPACK的BaseClass单元中,而不是我的应用程序中。我跟踪了这个问题,发现当我构建的过滤器图使用我的过滤器,播放过滤器时,问题就发生了。注意,我的过滤器正作为私有未注册过滤器直接合并到我的应用程序中,而不是作为外部AX。当TBCEnumPins.Next(),调用基类方法DirectShow时,如果ppPins参数为NIL,则会发生范围检查错误。由于我不是DirectShow专家,所以我不确定在不干扰DirectShow引脚枚举过程的适当流程的情况下修复此错误的正确方法。相反,如果这是一个不可忽略的真正错误条件,那么我需要知道在此事件中抛出的正确异常或返回的HRESULT代码。有人能告诉我调整这个代码为零ppPins参数的正确方法吗?完整的方法代码与发生范围检查错误的行如下,突出显示:

代码语言:javascript
复制
function TBCEnumPins.Next(cPins: ULONG; out ppPins: IPin; pcFetched: PULONG): HRESULT;
type
  TPointerDynArray = array of Pointer;
  TIPinDynArray = array of IPin;
var
  Fetched: cardinal;
  RealPins: integer;
  Pin: TBCBasePin;
begin
    // ATI: Debugging range check error.
    try
        if pcFetched <> nil then
          pcFetched^ := 0
        else
          if (cPins>1) then
          begin
            result := E_INVALIDARG;
            exit;
          end;
        Fetched := 0; // increment as we get each one.

        // Check we are still in sync with the filter
        // If we are out of sync, we should refresh the enumerator.
        // This will reset the position and update the other members, but
        // will not clear cache of pins we have already returned.
        if AreWeOutOfSync then
          Refresh;

        // Calculate the number of available pins
        RealPins := min(FPinCount - FPosition, cPins);
        if RealPins = 0 then
        begin
          result := S_FALSE;
          exit;
        end;

        {  Return each pin interface NOTE GetPin returns CBasePin * not addrefed
           so we must QI for the IPin (which increments its reference count)
           If while we are retrieving a pin from the filter an error occurs we
           assume that our internal state is stale with respect to the filter
           (for example someone has deleted a pin) so we
           return VFW_E_ENUM_OUT_OF_SYNC }

        while RealPins > 0 do
        begin
          // Get the next pin object from the filter */
          inc(FPosition);
          Pin := FFilter.GetPin(FPosition-1);
          if Pin = nil then
          begin
            // If this happend, and it's not the first time through, then we've got a problem,
            // since we should really go back and release the iPins, which we have previously
            // AddRef'ed.
            ASSERT(Fetched = 0);
            result := VFW_E_ENUM_OUT_OF_SYNC;
            exit;
          end;

          // We only want to return this pin, if it is not in our cache
          if FPinCache.IndexOf(Pin) = -1 then
          begin
            // From the object get an IPin interface
            TPointerDynArray(@ppPins)[Fetched] := nil; // <<<<<< THIS IS WHERE THE RANGE CHECK ERROR OCCURS.
            TIPinDynArray(@ppPins)[Fetched] := Pin;
            inc(Fetched);
            FPinCache.Add(Pin);
            dec(RealPins);
          end;
        end; // while RealPins > 0 do

        if (pcFetched <> nil) then pcFetched^ := Fetched;

        if (cPins = Fetched) then result := NOERROR else result := S_FALSE;
    except
        On E: Exception do
        begin
            OutputDebugString(PChar(
                '(TBCEnumPins.Next) Exception class name(' + E.ClassName + ') message: ' + E.Message
            ));

            raise;
        end;
    end;
end;

UPDATE:从技术角度看似乎很好,但从编码习语的角度看却有点奇怪,而且构造方式与范围检查不兼容。通过ppPins "out“参数传入的NIL映射到调用方传递给TBCEnumPins.Next()的目标缓冲区,作为ppPins参数。例如,下面的代码来自此页面:

http://tanvon.wordpress.com/2008/09/07/enumerating-the-directshow-filter-pin/

该页面上有以下代码,这些代码与DirectShow过滤器进行交互,以枚举过滤器的引脚:

代码语言:javascript
复制
IEnumPins * pEP;
pSF->EnumPins(&pEP);
IPin * pOutPin;
while(pEP->Next(1,&pOutPin,0) == S_OK)
{
    PIN_DIRECTION pDir;
    pOutPin->QueryDirection(&pDir);
    if(pDir == PINDIR_OUTPUT)
        break;// success
    pOutPin->Release();
}
pEP->Release();

通过告诉Next()方法要检索多少引脚,TBCEnumPins.Next()方法代码及其不寻常的动态数组转换是安全的,因为它只会按照下一个()函数"cPins“参数中请求的引脚数复制到ppPins " out”参数。只要调用方通过一个能够容纳"cPins“中请求的引脚数的目标缓冲区,一切都可以正常工作(只要关闭范围检查)。注意,在本例中,名为“IPin”的outPin变量是目标缓冲区。如果打开范围检查,则会发生范围检查错误,因为Delphi将零长度数组视为零。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-29 08:04:53

我怀疑您对ppPins参数的声明以及对TPointerDynArrayTIPinDynArray的转换。

如果您假装传入的数组是Delphi动态数组,而实际上并非如此,则会得到范围错误。Delphi动态数组在数组的第一项之前有一个内存块,并标识数组的引用计数及其长度。由于数组中缺少这个块,所以欺骗编译器,让它相信存在会导致您看到的错误。

我会这样做,如下所示。这与MSDN中使用的声明相匹配。您必须使用一些指针算法,但我相信这样做更容易,因为您的代码现在可以很容易地关联到您可以在网上找到的任何C++示例。

代码语言:javascript
复制
type
  PIPin = ^IPin;

function TBCEnumPins.Next(cPins: ULONG; ppPins: PIPin; pcFetched: PULONG): HRESULT;
begin
  ... 
  // this is the meat of the loop, assign to the output array, and increment
  Pointer(ppPins)^ := nil; // initialise the intf ref to avoid bogus _Release
  ppPins^ := Pin;
  inc(ppPins);
  ...
end;
票数 1
EN

Stack Overflow用户

发布于 2011-10-29 07:43:47

IEnumPins::Next方法不应该用ppPins == NULL调用。因此,最好的处理方法是立即返回E_POINTER错误代码。

您可能需要检查调用者的代码,以找出为什么在第一时间就有NULL

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7935896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档