Hello,
(I know this posting is long, but for someone with knowledge of COM my questions are easy to answer!)
I am trying to use smart pointers for the DirectX COM Interfaces to automatize the AddRef()/Release() calls.
To be more precise: I use the smart pointer _com_ptr_t for the ID3D10Effect interface. I create the smart pointer type with this macro:
_COM_SMARTPTR_TYPEDEF(ID3D10Effect, IID_ID3D10Effect);
Now there exists a type ID3D10EffectPtr.
I also have a little helper function that returns the ref count of the object:
template <typename T>
uint getRefCount(T& object) {
int ref = object->AddRef();
ref = object->Release();
return ref;
}
Additionally I have a class (EffectManager) to load effect files. The class has 2 member variables:
ID3D10EffectPool* mEffectPool;
ID3D10EffectPtr mSharedEffectPtr;
In a member function I load the effect pool:
D3DX10CreateEffectPoolFromFile(fullEffectFilename.c_str(), NULL, NULL, "fx_4_0",
HLSLFlags, 0, mDevice, NULL, &mEffectPool, &shaderErrors, NULL);
mSharedEffectPtr.Attach( mEffectPool->AsEffect() );
int c = getRefCount(mEffectPool); // c=1
c = getRefCount(mSharedEffectPtr); // c=1
This code loads the .fxh effect pool file and then grabs the effect and sets the smart pointer to the effect interface. In the last 2 lines I check the ref count for both mEffectPool and the smart pointer mSharedEffectPtr. Both have a value of 1. If I call AddRef() of either of them, the ref count of the other one also increases. This confuses me. Does this mean the effect pool and the effect of the pool is basically the same?
My other question: After I've loaded the pool, I load a normal effect file:
int c = getRefCount(mEffectPool); // c=1
c = getRefCount(mSharedEffectPtr); // c=1
HRESULT hr = D3DX10CreateEffectFromFile(fullEffectFilename.c_str(),NULL, NULL, "fx_4_0",
HLSLFlags, D3D10_EFFECT_COMPILE_CHILD_EFFECT, mDevice, mEffectPool,NULL, &effect, &shaderErrors, NULL);
c = getRefCount(effect); // c=1
c = getRefCount(mEffectPool); // c=2
c = getRefCount(mSharedEffectPtr); // c=2
ID3D10EffectPtr effectPtr(effect, false); // Line A
c = getRefCount(effect); // c=1
c = getRefCount(effectPtr); // c=1
c = getRefCount(mEffectPool); // c=2
c = getRefCount(mSharedEffectPtr); // c=2
mEffects[effectFilename] = effectPtr; // Line B
c = getRefCount(effect); // c=2
c = getRefCount(effectPtr); // c=2
c = getRefCount(mEffectPool); // c=3
c = getRefCount(mSharedEffectPtr); // c=3
After I loaded the effect, the variable effect has a ref count 1 and the pool and the smart pointer both have ref count of 2. Does this mean, that whenever I load an effect file, the ref count of the pool gets incremented?
In line A I define a smart pointer and initialize it with the effect. In the 4 lines after Line A I check the ref counts and they sound ok: The ref count of the effect didnt change, because variable effectPtr is now the owner of the effect. mEffectPool and mSharedEffectPtr didnt change too.
In line B I assign the effectPtr variable to a map map<string, ID3D10EffectPtr>. After line B the ref count of effect and effectPtr are 2. This sound logical, since the copy into the map lead to a AddRef() But the last 2 lines I dont understand. The ref count of mEffectPool and mSharedEffectPtr (the effect of the pool) raised to 3! Why does the ref count of the pool increase, when the effect ref count is increased??
Thanks for any ideas/help!