Re: SAFEARRAY issue.
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Fri, 14 Oct 2005 08:15:07 -0400
<subashjayan@xxxxxxxxx> wrote in message
news:1129283636.659307.202470@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
> int nCount = 3;
>
> SAFEARRAY *pSA;
> SAFEARRAYBOUND rgsaBound[1];
> rgsaBound[0].lLbound = 0;
> rgsaBound[0].cElements = nCount;
> long aLong = 1;
>
> pSA = SafeArrayCreate(VT_VARIANT, 1, rgsaBound);
This creates an array with valid indexes of 0, 1 and 2. Note that aLong
is initialized to 1, not to 0.
> for ( int n = 0; n < 3; n++)
> {
> VARIANT vOut;
> VariantInit(&vOut);
> vOut.vt= VT_BSTR; // set type
> vOut.bstrVal= ::SysAllocString(L"Test");
> if (FAILED( SafeArrayPutElement(pSA, &aLong, &vOut)))
> {
> VariantClear(&vOut); // release BSTR from memory on error
> SafeArrayDestroy(pSA); // does a deep destroy on error
> }
> VariantClear(&vOut); // does a deep destroy of source VARIANT
> aLong++;
> nCount++;
This loop accesses elements 1, 2 and 3. The last access fails, at which
point you destroy the array. But you don't set pSA to NULL, so it now
contains a dangling pointer.
> VariantInit(&p_udt->Special);
> p_udt->Special.vt= VT_SAFEARRAY|VT_BSTR; // set type
> p_udt->Special.parray = pSA ; // BSTR array
And here you pack the dangling pointer into a VARIANT.
> p_udt->lStatus =1;
> p_udt->lChannelNumber = 10;
> VariantClear(&p_udt->Special);
And here you cause double destruction. By the way, what's the point of
setting up a VARIANT and then clearing it immediately?
> }
> hr = ::SafeArrayUnaccessData( *SequenceArr );
>
> i could create all arrays. But while i am accessing the elements , the
> result is getting NULL, the erro code is "0x80070057"
> "The parameter is incorrect".
Now you know why.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: SAFEARRAY issue.
- From: subashjayan
- Re: SAFEARRAY issue.
- References:
- SAFEARRAY issue.
- From: subashjayan
- SAFEARRAY issue.
- Prev by Date: SAFEARRAY issue.
- Next by Date: Re: Error -2147319765
- Previous by thread: SAFEARRAY issue.
- Next by thread: Re: SAFEARRAY issue.
- Index(es):
Relevant Pages
|