Before discussing SuperMap, we sincerely hope that Microsoft can improve the Garbage Collector and help developers release objects more intelligently. First, let's take a look at the differences between the creation and release of traditional COM objects and. NET Framework object models: (1) The customer of the COM object must manage the lifetime of the COM object, and. the lifetime of the NET object is managed by the CLR (Common Language Runtime), that is, it is automatically reclaimed by the GC (Garbage Collection) mechanism. (2) The COM Object client uses a pointer to reference the COM object. The position of the object in the memory remains unchanged.. NET object resident in the memory.. NET Framework execution environment (execution environment) to manage, the object location in the memory is variable, for example, to optimize the performance, will update all references to the object at the same time. This is also based on the premise that the CLR does not use pointers. To put it simply, the. com client calls IUnKnown-> AddRef (), which is called every time an object is released IUnKnown-> Release (). Once the reference count reaches zero, the instance is released. In. NET, how does one completely Release? Fortunately, in. net, programmers are allowed to explicitly call the com Release method. This method is called System. Runtime. InteropServices. Marshal. ReleaseComObject.Currently, SuperMap is basically a COM Object in. NET. The main point of this discussion is how to completely release COM objects by writing them. Let's take a look at the code section: Private void axSuperMap1_Tracked (object sender, EventArgs e) { SoDatasetVector _ soDatasetVector = SuperWorkspace. Datasources [0]. Datasets [0] as soDatasetVector; SoRecordset _ soRecordset = _ soDatasetVector. Query ("1 = 2", true, null ,""); If (_ soRecordset. AddNew (axSuperMap1.TrackedGeometry, null) =-1) { MessageBox. Show ("failed to add data "); } System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soRecordset ); _ SoRecordset = null; System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soDatasetVector ); _ SoDatasetVector = null; } This code looks very concise and elegant. But it is not actually released completely. Now let's talk about the title of "Ten Thousand. For some objects that are implicitly returned, you must call ReleaseComObject in this way to release the objects correctly and ensure their normal operation. For some objects that are implicitly returned, you must call ReleaseComObject in this way to release the objects correctly and ensure their normal operation. For some objects that are implicitly returned, you must call ReleaseComObject in this way to release the objects correctly and ensure their normal operation. Now let's take a look at the complete writing of the above Code release: Private void axSuperMap1_Tracked (object sender, EventArgs e) { SoDataSources _ soDataSources = SuperWorkspace. Datasources; SoDataSource _ soDataSource = SuperWorkspace. CES [0]; SoDatasets _ soDatasets = _ soDataSource. Datasets; SoDataset _ Dataset = _ soDatasets [0]; SoDatasetVector _ soDatasetVector = _ Dataset as soDatasetVector; SoGeometry _ soGeometry = axSuperMap1.TrackedGeometry; SoRecordset _ soRecordset = _ soDatasetVector. Query ("1 = 2", true, null ,""); If (_ soRecordset. AddNew (_ soGeometry, null) =-1) { MessageBox. Show ("failed to add data "); } System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soGeometry ); _ SoGeometry = null; System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soRecordset ); _ SoRecordset = null; System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soDatasetVector ); _ SoDatasetVector = null; System. Runtime. InteropServices. Marshal. ReleaseComObject (_ Dataset ); _ Dataset = null; System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soDatasets ); _ SoDatasets = null; System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soDataSource ); _ SoDataSource = null; System. Runtime. InteropServices. Marshal. ReleaseComObject (_ soDataSources ); _ SoDataSources = null; } You are not mistaken. In fact, I am also very depressed. This writing is indeed very weak and cumbersome. But remember, if you don't want the product you write, the System. AccessViolationException will pop up when you don't think of it: Try to read or write the protected memory. This usually indicates that other memory is damaged ." The mistake is to be honest and use this weak statement. |