Jump to content


general direct x com question


2 replies to this topic

#1 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 19 December 2007 - 12:32 AM

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.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 19 December 2007 - 03:08 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 19 December 2007 - 04:52 AM

Awesome, that solves my problem. Thanks as always Reedbeta.

Gardon





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users