0
BSTR newID_x = SysAllocString(L"newID");
BSTR newX_x = SysAllocString(L"newX");

functionA(&newID_x);

//Func A does some operation on newID_x, we have that value in newID_x now

functionA(&newX_x);
//After Func A is called for the second time, both newID_x and newX_x become the same
//i.e, both are pointing to same locations and hence values too

My question is that, is it a correct behavior for BSTRs, do we need to save the newX_x in some new BSTR after calling functionA the first time?

Or is it wrong on part of functionA that it may be wrongly allocating/de-allocating the passed BSTRs.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
ammar
  • 1

1 Answers1

0

What you describe is "in-out" parameter semantics - the parameter is initialized prior to call, then while in the call it is changed and the change is visible to the caller. It is acceptable, but having such interface is not very convenient. In this case the callee will have to reallocate the BSTR and then pass ownership to the caller.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Can you suggest an alternative way of doing that? In my case if I dont initialize the newX_x or newID_x with SysAllocString and simply pass it to function A, will that be a better approach? Will it result in a different behavior after the return of function call? – ammar Jun 10 '11 at 19:30
  • @ammar: It depends on what semantics you want. You could for example have one "in" and one "out" parameter instead. It really depends on how you want to use that function. – sharptooth Jun 14 '11 at 05:55