PDA

View Full Version : general direct x com question


gardon
12-18-2007, 04:32 PM
I have an IDirectMusicPerformance8* object. If I pass that object into a function, and do the following, do I have to call release on the new pointer?:

void SetPerformance( IDirectMusicPerformance8 * performer )
{
IDirectMusicPerformance8* newPerformer = performer;
}


At the end of the code, I would have the original variable 'performer' that was passed in to the function stored elsewhere. I would call release on it, but would I also have to call release on the new variable I created and assigned to that outside variable?

Would I do the following?


newPerformer->Release();
performer->Release();



Or do I just set the 'newPerformer' variable I created in the function to NULL, and only release the first instance?

It's been a while since I've programmed...

Thanks in advance.

Reedbeta
12-18-2007, 07:08 PM
Release() decrements the COM object's internal reference counter, and destroys the object when the counter reaches zero.

The counter does *not* get incremented when you make a copy of the pointer to the COM object. COM isn't magic - it can't see into your code and look at what you do with your pointers. It only gets incremented when you call AddRef.

You can use AddRef/Release within your own application if you have multiple pointers to the same COM object and you're not sure about the lifetime of these pointers. But if you keep a pointer in some kind of application-object or such thing that you know will last as long as any other pointer to the COM object, you can just Release() it once in the destructor for your application-object, and you'll be fine.

gardon
12-18-2007, 08:52 PM
Awesome, that solves my problem. Thanks as always Reedbeta.

Gardon