tags:

views:

115

answers:

3

I have an application written in PHP that uses a COM dll written in C#. The dll creates an image file by combining two other image files. The PHP script then takes over to do the housekeeping tasks of deleting the two source files and renaming the resulting combined file.

The problem is the PHP script can't delete one of the source files because it's locked. The weird thing is that the process that has it locked is itself which in this case is the Apache Web Server.

I have tried altering the C# dll to dispose of all bitmap and graphics objects prior to exiting, and yet the lock remains. My question is, what can I do to get the dll to let go and release the file locks. This is very frustrating.

A: 

To be sure that you're definitely getting rid of all potential locks on the image, you could wrap your code in a using block.

using(Bitmap myBitmap = new Bitmap())
{
  //use myBitmap in here
}
Aaron
This technique worked better than anything else I've tried so far, but it took 26 seconds for the dll to finally release the lock. Is there a DisposeDammit() function?
It shouldn't be taking SO long to release the lock unless the algorithm that uses those images is taking that long. Are you doing this remotely? It could be a bandwidth issue, or it could be an algorithm issue where things aren't being done as efficiently as they should be.
Aaron
Ooops. I put in a sleep(25); in my php code for debugging purposes. It's now gone and the program is now working. I also added a unset($comobj); to tell php to release the comobj variable. Thanks for the help.
A: 

An application can definitely lock a file from itself. For example:

using(FileStream fs = File.Open("somefile.txt", FileMode.Open))
{
    File.Delete("somefile.txt"); // Exception: File already in use.
}

You need to make sure your all open handles to the file have been closed before your PHP file can clean up.

scottm
A: 

As a last resort you can use Unlocker utility (via command line) to unlock the files.

z-boss
I like unlocker, but this is a server based application, and the clients won't be able to interact with the unlocker gui via their web browser.
Of course I understand that. If you have Unlocker installed on your server your PHP code could call it (exec function).
z-boss
Sorry. I didn't read the last part of your sentence. Didn't know there was a command line way to get unlocker to do its magic. Good to know. Thanks.
No problem. When you reach 15 rep point you'll be able to say thank you simply by upvoting a useful answer or comment.
z-boss