2

I have an ArcMap extension in which I'm trying to persist a string value. When the document is saved, the public member m_LayerName in the application extension class is not being persisted. Either it's not being written to the stream or it's not being loaded correctly once the document is reopened. I'm not sure what I'm doing wrong. The Save and Load events are definitely being executed appropriately, but no data is being saved. This is my first attempt at persisting data with an extension so I'm missing something. Below is the relevant code.

Public m_LayerName As String

...

Public ReadOnly Property ID() As ESRI.ArcGIS.esriSystem.UID Implements ESRI.ArcGIS.esriSystem.IPersistVariant.ID
    Get
        Dim typeID As UID = New UIDClass()
        typeID.Value = Me.GetType().GUID.ToString("B")
        Return typeID
    End Get
End Property

Public Sub Load(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Load
    m_LayerName = CStr(Stream.Read())
    Marshal.ReleaseComObject(Stream)
End Sub

Public Sub Save(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Save
    Stream.Write(m_LayerName)
    Marshal.ReleaseComObject(Stream)
End Sub
1
  • 1
    Is this a classic COM IExtension implementation or an add-in? Add-ins have a different persistence pattern, where IPersistStream or IPersistVariant are not used.
    – Petr Krebs
    Commented Jan 25, 2011 at 21:09

1 Answer 1

1

Try it without calling releasecomobject on the stream, I think the app still needs to use the stream.

BTW, also I'd recommend adding a version property, it sure is a pain when you update an extension and there are mxds you're being asked to be backward compatible with.

8
  • I am quite intrigued.. not sure, but I do not believe this is the issue. ReleaseComObject and FinalReleaseComObject only affect the reference count managed by the CLR. When the CLR holds a reference to a COM object (creates its RCW), it calls IUnknown.AddRef exactly once, manages a reference count of its own, and as soon as it drops to 0, calls IUnknown.Release. When ArcMap is performing the serialization, it holds onto the stream object as well, and as such, releasing the CLR's single reference should not have any negative side effects.
    – Petr Krebs
    Commented Jan 24, 2011 at 23:54
  • And also (knowing one can never fully believe those samples), in some samples ESRI explicitly guides you to release the stream object, e.g. this article: resources.esri.com/help/9.3/ArcGISEngine/dotnet/…
    – Petr Krebs
    Commented Jan 24, 2011 at 23:56
  • @petr - I'm not for certain releasecomobject on the stream can cause problems, just that I never do that, and have never had a problem with any of persistable extensions I've written. Another possible explanation might be a guid collision, perhaps as a result of copying and pasting code from another IExtension class without changing the guid. Commented Jan 25, 2011 at 2:49
  • I tried it without the ReleaseCom to no avail. It seems like the data is simply not being written to the stream, like it's not associating it with the GUID.
    – Jeff Berry
    Commented Jan 25, 2011 at 16:12
  • This is a new extension, so there shouldn't be any duplicate GUID. I'll try a test new test project though to see what happens.
    – Jeff Berry
    Commented Jan 25, 2011 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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