I'm having a problem with ObjectData.
-- See code below for reference --
I'm calling the function attachODAndSetValue several times through a For statement (For each id in ObjectIDList).
Eventually, I alway get the following error:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The error always refer to the following line of my code: table.AddRecord(rec, id)
Before calling attachODAndSetValue, I make sure that the ObjectID passed to the function is valid (I check if id.isValid = True).
Did some of you had this kind of problem with ObjectData? Did you find a solution?
I'm using AutoCAD MAP 3D 2010 64bits on Windows 7
Thanks
Louis
Public Function attachODAndSetValue(ByVal tableName As String, ByVal id As ObjectId, ByVal dataCol As Collection) As Integer
'tableName : ObjectData Table to attach to the Object
'id : objectId of the element to which I want to attach the ObjectData
'dataCol : data to write in the ObjectData
Dim errStatus As Integer = 0
Dim rec As Record = Nothing
Try
Dim mapApp As Autodesk.Gis.Map.MapApplication = Autodesk.Gis.Map.HostMapApplicationServices.Application
Dim activeProject As Project.ProjectModel = mapApp.ActiveProject
Dim tableList As Tables = activeProject.ODTables
If tableList.IsTableDefined(tableName) = False Then
'table does not exist, exit try
errStatus = 2
Exit Try
End If
Dim table As ObjectData.Table = tableList.Item(tableName)
'Create and Initialize the record
rec = ObjectData.Record.Create
table.InitRecord(rec)
If rec.Count = dataCol.Count Then
Dim i As Integer
For i = 0 To rec.Count - 1
'Assign the value
rec.Item(i).Assign(dataCol.Item(i + 1))
Next
Else
errStatus = 3
Exit Try
End If
table.AddRecord(rec, id) 'Program crashed here
errStatus = 0
Catch err As MapException
MessageBox.Show("Error:" & vbNewLine & err.Message & vbNewLine & vbNewLine & err.ToString)
errStatus = 1
Finally
If rec IsNot Nothing Then
rec.Dispose()
End If
End Try
Return errStatus
End Function
Have you downloaded the Map ObjectARX SDK 2011? In the Docs folder, the sdk.arx.net.dev.chm file has a section on Object Data and Attaching. Quoting the doc:
"Each Item property in the record is of type Utilities.MapValue, which is a general-purpose class for storing data of variant types. To set any field, get a reference to the field from the Record object using the Item property. Assign the value with MapValue.Assign()."
Have you checked your datacol.item(i+1)? Does it exist? Is it valid for the field type (rec.item(i))?
Yes, the data type is OK
The problem occurs occasionally (8 out of 10 times).
Sometimes the program works, sometimes it doesn't.
The only way I found to make it work was to call the GarbageCollector at the end of attachODAndSetValue or to dispose of tableList, activeProject and mapApp. This make the program super SLOOWWW.
I also tried to create tableList, activeProject and mapApp outside attachODAndSetValue and pass tableList as an argument and it still doesn't work.
What is annoying is that program has been working perfectly for 4-5 years on an old P4 3GHz. When we got à new i7 940, last year, I started to have occasional problems. We just migrated to 64 bits and I can't make it run anymore...
I made a counter to display in which of the loop of the for that the program crashed... It's usually around the 400th loop (out of 1700).
Hello, I'm having the same problem when adding records ObjectData, also decided in the function call GC.Collect (), I use 64-bit AutoCAD MAP 2012, but this solution Deich preventing the application very slow work.
Can you help me solve this problem please?
Here is how I corrected to problem.
I added a parameter to the function : ODTableList as ObjectData.Tables.
I initialise this variable in the sub calling attachDefaultODAndSetValue
I had to do this modification to every single function using ObjectData but it's been working perfectly since.
Here is an example:
The sub calling attachODAndSetValue
Public Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttton.Click
Dim ODtableList As ObjectData.tables = Autodesk.Gis.Map.HostMapApplicationServices.Application.ActiveProject.ODTables
Dim myODTable As string = "myODTable"
Dim id As ObjectID
'set a value to id
'...
'...
Dim odCol As new Collection
'Add data to odCol
'...
'...
attachODAndSetValue(ODtableList, myODTable, id, odCol))
End Sub
New version of attachODAndSetValue
PublicFunction attachODAndSetValue(ByVal ODTablesList As ObjectData.Tables, ByVal tableName AsString, ByVal id As ObjectId, ByVal dataCol As Collection) AsInteger
Dim errStatus AsInteger = 0
Dim rec As Record = Nothing
Try
If ODTablesList.IsTableDefined(tableName) = FalseThen
errStatus = 2
ExitTry
EndIf
Dim table As ObjectData.Table = ODTablesList.Item(tableName)
rec = ObjectData.Record.Create
table.InitRecord(rec)
If rec.Count = dataCol.Count Then
Dim i AsInteger
For i = 0 To rec.Count - 1
rec.Item(i).Assign(dataCol.Item(i + 1))
Next
Else
errStatus = 3
ExitTry
EndIf
table.AddRecord(rec, id)
table.Dispose()
errStatus = 0
Catch err As MapException
MessageBox.Show("Errorr:" & vbNewLine & err.Message)
errStatus = 1
Finally
If rec IsNotNothingThen
rec.Dispose()
EndIf
EndTry
Return errStatus
EndFunction