10 Replies - 386 Views - Last Post: 09 February 2013 - 01:56 PM Rate Topic: -----

#1 RCQ Games  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 33
  • Joined: 28-March 09

Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 10:46 AM

Do i need to release COM objects in Directx11 if they are set to NULL?

For example in my code i have
//Variables initialization
ID3D11Device* d3d11Device = NULL;

//in my shutdown function
if(d3d11Device!=NULL){d3d11Device->Release();} 



Lets say an error occurs so my program calls my shutdown function and d3d11Device never get set a new value should it still be released?, because when i dont have the cleanup set up like that i get access violation which i presume are from d3d11Device being NULL

Is This A Good Question/Topic? 0
  • +

Replies To: Do i need to release COM objects if they are NULL

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,162
  • Joined: 12-June 08

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 10:57 AM

It wouldn't make much sense to not release a null object in normal operation, and then have to in some exception, right? So I would say 'no'.


Quote

because when i dont have the cleanup set up like that i get access violation which i presume are from d3d11Device being NULL

I would have to see the actual exception and the code it is happening in.
Was This Post Helpful? 1
  • +
  • -

#3 RCQ Games  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 33
  • Joined: 28-March 09

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 11:09 AM

Thanks i got rid of the if statements and restructured my shutdown function a little bit and now the access violations are gone, turns out i was looping through the shutdown function by accident releasing the COM objects twice and i think that was causing the access violation. Am I correct?

here is the error code i was getting:
Unhandled exception at 0x00715250 in DryIce.exe: 0xC0000005: Access violation executing location 0x00715250.


Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 04:43 PM

You only release something if it was successfully allocated in the first place. The Release() routine does no exception handling if the object is already NULL. Why? Because there are so many justifiable scenarios where it would be unwise for Release() to handle such an exception.
Was This Post Helpful? 1
  • +
  • -

#5 RCQ Games  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 33
  • Joined: 28-March 09

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 05:47 PM

so does that mean like above if no value besides NULL(which was set at initialization) is set to the COM object it does not need to be released?

I read somewhere that NULL apparently set the variable to no value not 0 but i cant remember if they were talking about readability of code or the actual functioning of code?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,162
  • Joined: 12-June 08

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 06:11 PM

Correct null can be thought of as 'no value'. 0 is a value.


https://en.wikipedia...er#Null_pointer
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 06:34 PM

The definition of NULL is implementation dependent. I've personally also seen it as '(void*) 0', '\0', etc.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,162
  • Joined: 12-June 08

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 06:42 PM

So null is now a cloud.. everyone sees it but sees something different in it. Eeek! Maaaaah datas are in the null void!
Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Do i need to release COM objects if they are NULL

Posted 08 February 2013 - 08:23 PM

View PostRCQ Games, on 08 February 2013 - 08:47 PM, said:

so does that mean like above if no value besides NULL(which was set at initialization) is set to the COM object it does not need to be released?

I read somewhere that NULL apparently set the variable to no value not 0 but i cant remember if they were talking about readability of code or the actual functioning of code?

Not sure what you mean. If you mean that you create and object and then set it to NULL without releasing it first then you are setting yourself up for a memory leak (memory that is allocated but your code knows nothing about because you removed the pointer to it without releasing it first).

That's a very serious error.
Was This Post Helpful? 0
  • +
  • -

#10 RCQ Games  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 33
  • Joined: 28-March 09

Re: Do i need to release COM objects if they are NULL

Posted 09 February 2013 - 01:40 PM

Ok thanks a memory leak is the last thing i want :), any ideas on the access i violation i posted above?
P.s. DryIce.exe is the name of my project
Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Do i need to release COM objects if they are NULL

Posted 09 February 2013 - 01:56 PM

Well, that's quite easy for you to find out. Place a breakpoint on

if(d3d11Device!=NULL)



hit 'F5' and see what value d3d11Device really is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1