Previous Thread:   How to control image size with sample grabber?

11/9/2005 8:27:50 AM    addsourcefilter locking file in windows
I'm new to this group and somewhat new to directshow programming.  I've  
  
been searching for a solution to this problem but I haven't found  
  
anything - I think this problem just comes from my lack of experience  
  
with windows programming.  
  
Here's the deal:  
  
I wrote a C++ MFC dll that I am calling from a VB6 application.  The  
  
point of the dll is to display a targa  or bitmap still file (.tga or  
  
..bmp) on a video output card.  The VB program invokes a method in the  
  
Dll that starts direct show, and builds a graph.  Then another method  
  
is invoked to that basically calls addsourcefilter where the source  
  
file is the .tga or .bmp file that I am displaying and then does  
  
"connectdirect" and runs the graph.  This all works.  The file is shown  
  
on the video output successfully.  Here is the problem.  Once  
  
addsourcefilter grabs hold of the .tga or .bmp file it never lets go  
  
until I quit the VB app and the dll. If I stop AND kill the graph,  
  
windows still sees the file as in use (I can test this by trying to  
  
change the filename in windows - I get an error message saying the file  
  
is in use.  
  
(this is especially difficult in while debugging in VB6 because I have  
  
to quit the IDE in order for windows to not see the file as in use any  
  
more).  Since the point of my application is to read an updated file  
  
with the same name - this is causing me all sorts of problems because  
  
the app (photoshop) that is writing the file can't overwrite it while  
  
it is in this "in use" condition.  Anyone have any thoughts? Again as I  
  
said I think this is just due to my inexperience with this.  My code  
  
(from the dll)is bellow: (thianks in advance for your help!)  
  
//This the method that does the displaying  
  
bool _stdcall displaynamedstillfile(BSTR bstrfilepath)  
  
{  
  
AFX_MANAGE_STATE(AfxGetStaticModuleState());  
  
HRESULT hr;  
  
//	BSTR bstrfilepath = filename.AllocSysString();  
  
if (getgraphmode() == 0){  
  
return false;  
  
}  
  
if (sInputFileFilter != NULL)  
  
{  
  
if (disconnectgraph() == false){  
  
return false;  
  
}  
  
}  
  
//add file to the graph  
  
//this  is where the file becomes locked  
  
// in windows as you might expect  
  
hr = sGraph->AddSourceFilter(bstrfilepath, bstrfilepath,  
  
&sInputFileFilter);  
  
if(FAILED(hr))  
  
{  
  
return false;  
  
}  
  
sFileOut = FindPin(sInputFileFilter, PINDIR_OUTPUT, NULL);  
  
if(sFileOut == NULL || sDecklinkIn == NULL)  
  
{  
  
SAFE_RELEASE(sFileOut);  
  
return false;  
  
}  
  
//hr = sGraph->Connect(sFileOut, sDecklinkIn);  
  
hr = sGraph->ConnectDirect(sFileOut, sDecklinkIn, NULL);  
  
if(FAILED(hr))  
  
{  
  
SAFE_RELEASE(sFileOut);  
  
return false;  
  
}  
  
hr = sControl->Run();  
  
if(FAILED(hr))  
  
{  
  
SAFE_RELEASE(sFileOut);  
  
return false;  
  
}  
  
Sleep(3);  
  
hr = sControl->Stop();  
  
return true;  
  
}  
  
//the following method does release the  
  
//video output card but the file is still locked  
  
// disconnect the file from the graph  
  
bool _stdcall disconnectgraph(void)  
  
{  
  
AFX_MANAGE_STATE(AfxGetStaticModuleState());  
  
HRESULT hr;  
  
if (getgraphmode() == 0){  
  
return false;  
  
}  
  
if (sInputFileFilter != NULL)  
  
{  
  
hr = sGraph->Disconnect(sDecklinkIn);  
  
if(FAILED(hr))  
  
{  
  
return false;  
  
}  
  
hr = sGraph->RemoveFilter(sInputFileFilter);  
  
if(FAILED(hr))  
  
{  
  
return false;  
  
}  
  
SAFE_RELEASE(sInputFileFilter);  
  
}  
  
fileIsConnected = false;  
  
return true;  
  
}  
  
// kill the whole graph  
  
bool _stdcall releasegraph(void)  
  
{  
  
AFX_MANAGE_STATE(AfxGetStaticModuleState());  
  
SAFE_RELEASE(sGraph);  
  
SAFE_RELEASE(sControl);  
  
SAFE_RELEASE(sEvent);  
  
SAFE_RELEASE(sMS);  
  
SAFE_RELEASE(sMP);  
  
SAFE_RELEASE(sKeyer);  
  
SAFE_RELEASE(sBV);  
  
SAFE_RELEASE(sBA);  
  
SAFE_RELEASE(sDeckVidRender);  
  
SAFE_RELEASE(sInputFileFilter);  
  
SAFE_RELEASE(sFileOut);  
  
SAFE_RELEASE(sDecklinkIn);  
  
currentKeyerMode = 0;  
  
fileIsConnected = false;  
  
graphIsBuilt = false;  
  
return true;  
  
}  
  
//safe release is  
  
// #define SAFE_RELEASE(p)     { if(p) { (p)->Release(); (p)=NULL; } }



11/10/2005 8:55:49 AM    Re: addsourcefilter locking file in windows
On 9 Nov 2005 08:27:50 -0800, means wrote:  
  
U haven't read your code, but a few comments.  
  
1.	This is MOST likely caused by a reference counting bug.  So there is one  
  
more AddRef than release.  MY first piece of advice is to rewrite with  
  
smart pointers.  That has a good chance of just fixing the problems.  
  
2.	Check out all the bits that get addrefed 'silently' - for example  
  
PinInfo and FilterInfo both addref bits.  
  
4.	Simplify the code.  Just build the graph (as simply as possibly - with  
  
AddSourceFilter and RenderStream) and then Release the graph afterwards.  
  
5.	Try another Filter.  The PushSOurce sample filter will do a bit of what  
  
you want.  So use that instead (even if on a different file for now) and  
  
see if it does the same.  If it does put a trace in the Release / AddRef  
  
and see if actually releases as many times as it AddRefs and hten debug  
  
that.  
  
Iain  
  
--  
  
Iain Downs (DirectShow MVP)  
  
Commercial Software Therapist  
  
www.idcl.co.uk

11/20/2005 2:01:17 PM    Re: addsourcefilter locking file in windows
Thanks for the advice.  I did some further testing and I found that  
  
graphedit exhibits the same problems.  Once you render a still file in  
  
graphedit Windows sees the file as "in use" until you exit graphedit  
  
completely (clearing the graph and starting a new one doesn't help -  
  
you have to exit graphedit). Since this seems to indicate a problem  
  
with the filters (as you suggested) rather than my code  -- and because  
  
of the limited time I have to implement this -- I decided to take a  
  
quick workaround approach.  I now just make a copy of the file before I  
  
render it.  It's not pretty at all but it does work for what I need.  
  
Thanks  
  
Iain wrote:

11/21/2005 1:22:00 PM    Re: addsourcefilter locking file in windows
On 20 Nov 2005 14:01:17 -0800, means wrote:  
  
Pretty only matters if people realise ... <g>  
  
Iain  
  
--  
  
Iain Downs (DirectShow MVP)  
  
Commercial Software Therapist  
  
www.idcl.co.uk