|
|
As mentioned above, I would like to ask those who expert in this area can help me to solve my problem.
I am currently using Vista SDK to develop my program which is able to copy songs repeatedly.
When I run my program repeatedly, the program hang with window dialog box that stated that:
"The instruction at "0x7c93426d" referenced memory at "0x26d902d". The memory could not be "read""
I know this may be a pointer issue that reference to faulty memory address but I sitll couldn't figure out the root cause. Can anybody give me some guidance?
Besides that, I would like to ask several question. Question 1: Example:
HRESULT GetRequiredPropertiesForAllContentTypes( IPortableDeviceValues* pObjectProperties, LPCWSTR wszParentObjectID) { HRESULT hr = S_OK; if (pObjectProperties == NULL) { hr = E_POINTER; return hr; }
--------------------------------------------------- is the condition of the if() is enough to ensure the pObjectProperties does not point to faulty address.
|
|
[Quoted Text] > --------------------------------------------------- > is the condition of the if() is enough to > ensure the pObjectProperties does not point to faulty address.
Sadly, not :( The pointer may point to 'somewhere' different than NULL but still being a wrong address (because the memory/object pointed has been freed for instance).
It is a good idea to use the calss CComPtr (aka "intelligent pointers"), as it automatically releases the interfaces when objects go out of scope, and asserts in debug mode in some buggy situations. ex: CComPtr<IWMWriter> pWriter;
hr=WMCreateWriter(NULL, &pWriter);
/*do whatever you want with your writer and don't bother calling pWriter->Release as it is automatically done when pWriter goes out of scope.*/
Vegethalia --------------------------------------- http://www.plethades.com
|
|
Example: function1(){ CComPtr<IPortableDeviceValues> pDevice;
hr=pDevice->SetStringValues(XXX, YYY); return; }
Main(){ function1(); } -------------------------------------------------- Question1: According to my understanding, pDevice will be release when return from function1 to Main(), right?
Question2: What if I need to pass information of an interface in Function1() to the Main(), so what should I do? For example: After I call SetStringValues(XXX, YYY), I need to pass the information I set to Main(). When it returns to Main(), the interface already release and the info I set will be gone, right?
|
|
payne ha escrit:
[Quoted Text] > Example: > function1(){ > CComPtr<IPortableDeviceValues> pDevice; > > hr=pDevice->SetStringValues(XXX, YYY); > return; > } > > Main(){ > function1(); > } > -------------------------------------------------- > Question1: > According to my understanding, pDevice will be release when return from > function1 to Main(), right?
In your example, you are not retrieving an object anywhere. I mean, pDevice will be NULL as it is not assigned anywhere... I have not worked with this particular interface, but i suppose that you must call another method to obtain a pointer to it (pAnotherObject- >AnyMethod(blabla, bla, bla, &pDevice)), or create a new one with CoCreate and some CLSID like CLSID_PortableDeviceValues.
In the later case, if you create a new instance, when your object goes out of scope all your values will be lost. If the former case, just the pointer will be release. All the properties set will remain.
And yes, pDevice will be released automatically. You can force the release if you need to reuse your object by calling pDevice.Release(). It is safe to call it even if the pointer is NULL.
> Question2: > What if I need to pass information of an interface in Function1() to the > Main(), so what should I do? > For example: > After I call SetStringValues(XXX, YYY), I need to pass the information I set > to Main(). When it returns to Main(), the interface already release and the > info I set will be gone, right?
You can do this:
HRESULT function1(CComPtr<IPortableDeviceValues> &pDevice, somevalues, somevalues) { return pDevice->SetStringValue(key, _T("Hello I'm a text")); }
main() { CComPtr<IPortableDeviceValues> pDevice; HRESULT hr;
//example with CoCreate hr=CoCreateInstance(CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER, IID_PortableDeviceValues, (void**)&pDevice);
//if collection is obtained from another method: //hr=pOtherObject->GetSomething(,,,,,&pDevice);
hr=function1(pDevice, somevalues, somevalues...); // and pDevice will maintain all the values already set in the function.
//work with pDevice here as it is not released yet. }
Hope this helps... :)
Vegethalia ----------------------------- http://eqsearchpro.pleyhades.com
|
|
Thank you. Your explaination helps a lot.
Sorry, my example is not the actual code. As you mentioned, before use the method SetStringvalue() under the interface IPortableDeviceValues, CoCreateInstance() is needed to create the object which pointed by pDevice.
And you answer to my question really helps.
|
|
|