Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have an ArcObject Dissolve procedure that creates a shp in a directory. This dissolved layer is a temp layer and is not added into ArcMap. I would like to delete it when the user clicks a button again, however it will not delete because their is a lock file (Dissolved.shp.6H8Z5D1.3448.1520.sr.lock) in the directory that happens after the dissolve gp runs:

pSumTable = pGeoProc.Dissolve(pInputTable, True, "ROUTE", "Dissolve.Shape, Minimum.ROUTE", pdatasetname2)

I looked at Using Scheme Locks, however I'm not using this layer to perform any edits. I also found this post on GIS SE, however it too does not seem to apply for my situation. How do I remove this sr lock file (my entire code below), while keeping the ArcMap session open?

Thanks

 Try

            'delete all temp files
            Dim s As String
            For Each s In System.IO.Directory.GetFiles("J:\20111030\GIS\Scripts\TempData2")
                System.IO.File.Delete(s)
            Next s

            'dissolve route layer
            Dim pMap As IMap

            pMap = mxdoc.FocusMap

            Dim pInputFeatLayer As IFeatureLayer

            pInputFeatLayer = featurelayer

            If pInputFeatLayer Is Nothing Then

                MessageBox.Show("The testinline layer was not found")
                Exit Sub

            End If

            Dim pInputTable As ITable
            pInputTable = pInputFeatLayer

            If pInputTable Is Nothing Then

                MsgBox("Table QI failed")
                Exit Sub

            End If

            If pInputTable.FindField("ROUTE") = -1 Then

                MsgBox("There must be a field named MUSYM layer ")
                Exit Sub

            End If

            Dim pInputFeatClass As IFeatureClass
            pInputFeatClass = featurelayer.FeatureClass

            Dim pfeatureclassname As IFeatureClassName
            pfeatureclassname = New FeatureClassName

            With pfeatureclassname

                .FeatureType = esriFeatureType.esriFTSimple
                .ShapeFieldName = "Shape"
                .ShapeType = pInputFeatClass.ShapeType

            End With

            Dim pNewWSName As IWorkspaceName
            pNewWSName = New WorkspaceName

            pNewWSName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"
            pNewWSName.PathName = "J:\20111030\GIS\Scripts\TempData2\"

            Dim pdatasetname2 As IDatasetName

            pdatasetname2 = pfeatureclassname
            pdatasetname2.Name = "Dissolved"

            pdatasetname2.WorkspaceName = pNewWSName

            Dim pGeoProc As IBasicGeoprocessor
            Dim pSumTable As ITable
            pGeoProc = New BasicGeoprocessor
            pSumTable = pGeoProc.Dissolve(pInputTable, True, "ROUTE", "Dissolve.Shape, Minimum.ROUTE", pdatasetname2)
            Dim pOutputFeatClass As IFeatureClass
            pOutputFeatClass = pSumTable

            If pOutputFeatClass Is Nothing Then
                MsgBox("FeaturesClass QI Failed")
                Exit Sub
            End If

            Dim pOutputFeatLayer As IFeatureLayer
            pOutputFeatLayer = New FeatureLayer
            pOutputFeatLayer.FeatureClass = pOutputFeatClass
            pOutputFeatLayer.Name = pOutputFeatClass.AliasName

            ' CLEAN UP
            featurelayer = Nothing
            pInputFeatLayer = Nothing
            pInputTable = Nothing
            pInputFeatClass = Nothing
            pfeatureclassname = Nothing
            pNewWSName = Nothing
            pdatasetname2 = Nothing
            pGeoProc = Nothing
            pSumTable = Nothing
            pOutputFeatClass = Nothing
            pOutputFeatLayer = Nothing

            pMap = Nothing
            mxdoc = Nothing

        Catch ex As Exception
            MessageBox.Show("Caught an unspecified error in the calling code: " & vbCrLf & ex.ToString)
        End Try
share|improve this question
    
I have had issues deleting files that are inside a workspace I still am maintaining a reference to. I typically run a garbage collection after nullifying any remaining references to the workspace and it will delete properly. Oddly enough, it sometimes maintains a schema lock, but if I run a tight loop with repeated delete attempts after a short timer, it will work. –  Nathanus Jan 9 '12 at 16:51
    
@Nathanus, Thanks for your reply. Do you have any code examples you could post using your method? I'm guessing I have to remove reference either to pSumTable or pdatasetname2? –  artwork21 Jan 9 '12 at 20:28
add comment

2 Answers

up vote 4 down vote accepted

Use System.Runtime.InteropServices.Marshal::ReleaseComObject to destroy the ArcObjects before you disassociate their reference.

share|improve this answer
add comment

To expand upon my comment, @artwork21 (which stems from this question), the code I used looks like this. I apologize for it being in C#, but I am not familiar enough with vb.net to provide a proper conversion.

private void deleteOldData(IFeatureWorkspace fWorkspacee)
    {
        IDataset olddataset = CheckForTempData(fWorkspace as IWorkspace);
        int i = 0;
        while (olddataset != null)
        {
            fWorkspace = null; //This should remove the lock that prevents deletion
            GC.Collect();
            try
            {
                olddataset.Delete();
                olddataset = null;
            }
            catch (Exception err)
            {
                i++;
                Thread.Sleep(100); 
                //There seems to be a delay in lock removal, so this is added as a stopgap
                if (i >= 10)
                {
                    setnttext("Error encountered deleting old temp data. Breaking Process.\n" + err.Message);
                    return;
                }
            }
        }
        return;
    }

The ReleaseComObject method is something used to remove all references to a COM object so it can be released from memory, which should remove all locks. I have had spotty luck with it of late, for whatever reason, but here is some code (again, in C#) that has worked in the past.

IWorkspace workspace = CreateFileGdbWorkspace(gdbpath, gdbtext);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workspace);

I do not believe you can use FinalReleaseComObject or ReleaseComObject after you set a reference to null however. I think I remember that causing an error about non-existent COM objects in my program. In your case, you would simply use ReleaseComObject on all of the variables you set to nothing before you remove the reference. Note that FinalReleaseComObject performs the looping that you see in the help document you linked in @Jim B's answer. It does the release over and over until the reference count becomes 0.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.