DeleteLayer from TOC does not release the memory.

852
9
07-19-2011 06:17 AM
GustavoFerrero
New Contributor
Hi All,

I've been trying to delete the layer from the map and release the memory but I was not able to find the solution.

I'm using the following code to remove the layer from the Map.

ILayer layer =  (ILayer) m_mapControl.CustomProperty;            
m_mapControl.Map.DeleteLayer(layer); 


This code is working however the memory is not released.

Does anyone know how can I release the memory? This is a big problem because when I add a lot of layer to the TOC and then I remove them the application does not release the memory.
0 Kudos
9 Replies
grahamwood
New Contributor III
Sorry no answer
but i have seen the same problem, also the locks are also still in place.
0 Kudos
AlexanderGray
Regular Contributor II
I suggest you try using the COMReleaser to explicitly release the resource from either the layer or the featureclass that is the layer source.
0 Kudos
NeilClemmons
Regular Contributor III
Before removing the layer from the map you should QI over to IDataLayer2 and call Disconnect.
0 Kudos
GustavoFerrero
New Contributor
Thanks for response,

I've tried disconnecting the layer, but It didn't work.

IDataLayer2 dl = (IDataLayer2)pFeaLayer;
dl.Disconnect();

m_mapControl.Map.DeleteLayer(layer);
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeaLayer) > 0) { };


I've made some tests into the ArcMap and it's happen the same problem. I think this is an ESRI issue.

Thanks.
0 Kudos
AlexanderGray
Regular Contributor II
I had this conversation, I think it was version 8.1 something, apparently ArcMap keeps a reference to the featureclass so it can re-add it faster if you chose undo...
Have you tried the com releaser on the featurelayer.featureclass?
0 Kudos
NeilClemmons
Regular Contributor III
I haven't tested anything to see what happens but calling the com releaser on the feature class might hose ArcMap's reference to it (I've had this happen in other situations).  Before releasing the feature class you may want to either clear the operation stack or remove the last operation (which should be the deleting of the layer) so that ArcMap isn't hanging on to it anymore.  You can access the operation stack via IMxDocument.OperationStack.
0 Kudos
AlexanderGray
Regular Contributor II
If I am not mistaken the original problem was with a map control, so probably engine.  But be careful if you have any other references to the featureclass in your code too because they will be just as hosed. 
As for doing it in ArcMap, I wouldn't recommend it unless you have a compelling reason (I had to do it for a file workspace because ArcMap would cache the csv file schema and wouldn't refresh even if the layer was removed, updated (with new fields) and re-added.)  I find ArcMap becomes somewhat unstable if it runs for more than a few days (some times a lot less) so I find shutdown as part of the workflow beneficial.
0 Kudos
GustavoFerrero
New Contributor
Thanks for reply,

The problem is happening in ArcMap however I need to fix it with the ArcEngine. I've been trying to read the OperationStack property but the value is null.

Do you have any other idea?

But the way I'm testing the following code (which is not working :() 


           ILayer layer =  (ILayer) m_mapControl.CustomProperty;
            // m_mapControl.Map.DeleteLayer(layer);

            IFeatureLayer pFeaLayer = (IFeatureLayer)layer;
            IFeatureClass pFeaClass = pFeaLayer.FeatureClass;

            IDataset pFCDataset = (IDataset)pFeaClass;
            IWorkspace pWorkspace = pFCDataset.Workspace;
            IDataset pWsDataset = (IDataset)pWorkspace;

            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeaClass) > 0) { };

            //disconnect and delete the layer from the map
            IDataLayer2 dl = (IDataLayer2)pFeaLayer;
            dl.Disconnect();

   m_mapControl.Map.DeleteLayer(layer);
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeaLayer) > 0) { };

            layer = null;
            System.GC.Collect();  


Thanks.
Gustavo.
0 Kudos
sapnas
by
Occasional Contributor III
Did you check m_mapControl.CustomProperty value after ReleaseComObject execution? You may have to use FinalReleaseComObject to release all  the references.
0 Kudos