[Accessibility] [Accessibility-ia2] Newbie question: help withBSTR arrays

Brian Cragun cragun at us.ibm.com
Wed Feb 24 09:18:38 PST 2010


Rob,

Thanks for your review of the code.  Your points are well made.   I was 
aware the return checks were sparse at best, but your pointing this out 
makes me realize I should maybe be much more meticulous about documenting 
it.    Your suggestion is good of freeing the memory in the responsible 
code so it is kept together.

Thanks

- Brian

Brian Cragun
IBM AbilityLab Consultant
Human Ability & Accessibility Center
www.ibm.com/able & w3.ibm.com/able
W:(720)-663-2801    H:(507)288-2437

Subscribe to RSS feed and contribute toAccessibility News communityon 
Connections
IBM Accessibility on Facebook ▼ IBMAccess on Twitter ▼ IBM Accessibility 
on LinkedIn




From:
"Rob Gallo" <rgallo at FreedomScientific.com>
To:
<pete at a11ysoft.com>
Cc:
accessibility at lists.linux-foundation.org, "'IA2 List'" 
<Accessibility-ia2 at lists.freestandards.org>
Date:
02/24/2010 09:48 AM
Subject:
Re: [Accessibility] [Accessibility-ia2] Newbie question: help   withBSTR 
arrays
Sent by:
accessibility-bounces at lists.linux-foundation.org



Brian,
 
On the client side, you need more error checking. You don't check the 
value of res after some calls. And you don't check to make sure the 
returned out parameters are valid. Any one of these things can go wrong. 
And you never know what you'll get back from someone else's code.
 
Likewise, on the server side, you don't check the input parameters to make 
sure they're non-null. And you don't check to make sure that the user 
hasn't requested fewer key bindings than you provide.
 
There is at least one case when you don't initialize a local variable, 
which you should always do, even when you're passing that variable to a 
function using it as an out parameter. That allows you to be more 
confident in correct checking of the return value of the parameter.
 
Also, if I were doing this, I would not free the BSTR's in the Process 
function. It's easy to lose track of allocating and freeing memory. So, I 
would have Process take a const wchar_t*, and have the function that 
requested the actions free each BSTR in turn. That way, you can clearly 
see where the memory was allocated (in the call to get the actions) and 
where it is freed. This will give less responsibility to the Process 
function, and make the memory allocation and deallocation easier for the 
next person to understand.
 
Thanks,
RG
 
 

From: accessibility-ia2-bounces at lists.linuxfoundation.org [
mailto:accessibility-ia2-bounces at lists.linuxfoundation.org] On Behalf Of 
Pete Brunet
Sent: Tuesday, February 23, 2010 6:43 PM
Cc: accessibility at lists.linux-foundation.org; IA2 List
Subject: Re: [Accessibility-ia2] [Accessibility] Newbie question: help 
withBSTR arrays

Hi Brian, I suspect there are some important readers that don't watch this 
list.  I'm cc'ing it to the IA2 list.  -Pete

Brian Cragun wrote: 
Thanks for your interest and patience.   The following code seems to be 
working.  It shows a CLIENT fragment that calls for the key bindings and 
then pulls them off for processing.   The SERVER snippet shows how I am 
building a simple three element BSTR array.    Please advise if you see 
any stupid errors being made. 

Thanks! 

Brian 

CLIENT: 
                        res = 
m_spAccessible2->QueryInterface(IID_IAccessibleAction,(void
**)&m_spAccessibleAction); 
                        long nActions = 0L; 
                        res = m_spAccessibleAction->nActions(&nActions); 
                        if(SUCCEEDED(res)){ 
                                BSTR name; 
                                BSTR description; 
                                for (int iAction=0 ; iAction<nActions; 
iAction++){ 
                                        res = 
m_spAccessibleAction->get_name(iAction, &name); 
                                        if(SUCCEEDED(res)){ 
                                                Process(name);  // Process 
the name and free the string 
                                        } 
                                        res = 
m_spAccessibleAction->get_description(iAction, &description); 
                                        if(SUCCEEDED(res)){ 
                                                Process(description); // 
Process the name and free the string 
                                        } 

                                        BSTR *aKeyBindings; 
                                        long aNumMaxBinding = 10; 
                                        long aNumBindings; 
                                        res = 
m_spAccessibleAction->get_keyBinding(iAction, aNumMaxBinding, 
&aKeyBindings, &aNumBindings); 
                                        if(SUCCEEDED(res)){ 
                                                for (int jBind=0; 
jBind<aNumBindings; jBind++){ 
 Process(aKeyBindings[jBind]); // Process the name and free the string 
                                                } 
 CoTaskMemFree(aKeyBindings); 
                                        } 

                                } 
                        } 

SERVER: 
STDMETHODIMP AccServer::get_keyBinding(long aActionIndex, long 
aNumMaxBinding, 
                                  BSTR **aKeyBinding, 
                                  long *aNumBinding) 
{ 

    *aKeyBinding = static_cast<BSTR*>(CoTaskMemAlloc((aNumMaxBinding) * 
sizeof(BSTR*))); 
        if (!*aKeyBinding) return E_OUTOFMEMORY; 

                *aKeyBinding[0] = ::SysAllocString(OLESTR("0 key binding"
)); 
//                  if (!*(aKeyBinding[0])) return E_OUTOFMEMORY; 
                (*aKeyBinding)[1] = ::SysAllocString(OLESTR("1 key 
binding")); 
//                  if (!*(aKeyBinding[1])) return E_OUTOFMEMORY; 
                (*aKeyBinding)[2] = ::SysAllocString(OLESTR("2 key 
binding")); 
//                  if (!*(aKeyBinding[2])) return E_OUTOFMEMORY; 
        *aNumBinding = 3L; 
  return S_OK; 
} 



Brian Cragun 
IBM AbilityLab Consultant
Human Ability & Accessibility Center
www.ibm.com/able & w3.ibm.com/able 
W:(720)-663-2801    H:(507)288-2437 

Subscribe to RSS feed and contribute toAccessibility News communityon 
Connections 
IBM Accessibility on Facebook ▼ IBMAccess on Twitter ▼ IBM Accessibility 
on LinkedIn



From: 
Brian Cragun/Rochester/IBM at IBMUS 
To: 
accessibility at lists.linux-foundation.org 
Date: 
02/22/2010 01:52 PM 
Subject: 
[Accessibility] Newbie question: help with BSTR arrays 
Sent by: 
accessibility-bounces at lists.linux-foundation.org




I'm struggling to understand how a BSTR array is properly allocated and 
content is added to it.   Also how the BSTRs in the array are retrieved. 
Does anyone have any simple examples? 

The information at 
http://accessibility.freestandards.org/a11yspecs/ia2/docs/html/_generalinfo.html#_arrayConsideration 
is useful but not quite enough for me. 
I assume I need to allocation the memory with CoTaskMemAlloc and release 
it with CoTaskMemFree. 

Thanks! 

- Brian 

Brian Cragun 
IBM AbilityLab Consultant
Human Ability & Accessibility Center
www.ibm.com/able & w3.ibm.com/able 
W:(720)-663-2801    H:(507)288-2437 
_______________________________________________
Accessibility mailing list
Accessibility at lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/accessibility


_______________________________________________
Accessibility mailing list
Accessibility at lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/accessibility
 

-- 
Pete Brunet
                                                                 
a11ysoft - Accessibility Architecture and Development
(512) 238-6967 (work), (512) 689-4155 (cell)
Skype: pete.brunet
IM: ptbrunet (AOL, Google), ptbrunet at live.com (MSN)
http://www.a11ysoft.com/about/
Ionosphere: WS4G _______________________________________________
Accessibility mailing list
Accessibility at lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/accessibility

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux-foundation.org/pipermail/accessibility/attachments/20100224/38424d1b/attachment-0001.htm 


More information about the Accessibility mailing list