我在 Windows XP/32 机器上使用 DSPACK DirectShow 组件库在 Delphi 6 中编写了一个自定义推送源过滤器。在编码期间,在我的应用程序编译器选项中打开范围检查后,我遇到了属于 DSPACK (BaseClass.pas) 的BaseClass单元中发生范围检查错误的问题。
该错误发生在我的 EnumPins() 方法期间。请注意,此方法驻留在 DSPACK 的 BaseClass 单元中,而不是我的应用程序中。我跟踪了问题并发现它发生在我构建的使用我的过滤器的过滤器图播放过滤器时。请注意,我的过滤器作为私人未注册过滤器直接合并到我的应用程序中,而不是作为外部 AX。当 DirectShow 调用基类方法TBCEnumPins.Next()时,如果ppPins参数为NIL然后发生范围检查错误。由于我不是 DirectShow 专家,我不确定在不干扰 DirectShow 引脚枚举过程的正常流程的情况下修复此错误的正确方法是什么。相反,如果这是一个不可忽略的真正错误条件,那么我需要知道在此事件中要抛出的正确异常或要返回的 HRESULT 代码是什么。谁能告诉我为 NIL ppPins 参数调整此代码的正确方法?完整的方法代码如下,其中出现范围检查错误的行突出显示:
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;
更新:从技术角度来看,DSPACK 代码似乎是合理的,但从编码习惯的角度来看有点奇怪,并且其结构与范围检查不兼容。通过 ppPins“out”参数传入的 NIL 映射到调用者作为 ppPins 参数传递给 TBCEnumPins.Next() 的目标缓冲区。例如,下面的代码来自这个页面:
http://tanvon.wordpress.com/2008/09/07/enumerating-the-directshow-filter-pin/
在该页面上是以下代码,它与 DirectShow 过滤器交互以枚举过滤器的引脚:
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() 方法代码及其不寻常的动态数组强制转换是安全的,因为它只会复制到 ppPins“out”参数中的引脚数在 Next() 函数“cPins”参数中请求。只要调用者传递一个可以保存“cPins”中请求的引脚数量的目标缓冲区,一切都可以正常工作(只要关闭范围检查)。请注意,在这种情况下,名为“outPin”的 IPin 变量是目标缓冲区。如果打开范围检查,则会发生范围检查错误,因为 Delphi 将 NIL 视为零长度数组。