AcadMap object data access leads to memory leak?

Discussion in 'AutoCAD' started by Norman Yuan, Feb 16, 2005.

  1. Norman Yuan

    Norman Yuan Guest

    This post is for those who do VBA in AcadMAP to manipulate object data.

    Situation:

    I am doing a VBA work that retrieving AcadMap object data into
    database/file. I have done similar things several times. Only this time
    there is a lot more object data to be extracted: about 40,000 polygons with
    object data attached, these object data belongs to 10 object data tables and
    each table has up to 40 fields.

    No problem for me to write code to loop through each polygon and each map
    object data table to retrieve all object data values and then save to
    database/file.

    The problem is that if object data attached to a polygon is accessed for
    retrieving, the computer memory usage increases and the program crashes
    eventually before the processing is done (I have pretty good computer: P4
    3.4 MHz + 1G memory, AutoCAD Map5 - 2002). The task manager indicates near
    3G memory (1G memory + near 2G virtual memory) was used at crash point.

    The code (and see my comments between code):

    Option Explicit

    Public Sub MapObjectData_Extract_Test()

    Dim aMap As AutocadMAP.AcadMap
    Dim mProj As AutocadMAP.Project

    Set aMap =
    ThisDrawing.Application.GetInterfaceObject("AutoCADMap.Application")
    Set mProj = aMap.Projects(ThisDrawing)

    Dim tbl As ODTable
    For Each tbl In mProj.ODTables
    ExtractMapData tbl ''' Here to extract object data under each
    object data table
    If MsgBox(tbl.Name & " is done." & vbCr & vbCr & _
    "Do you want to continue?", vbYesNo) = vbNo Then Exit For
    DoEvents ''Ensure that I can stop the long looping by clicking STOP
    button in VBA
    Next

    End Sub

    Private Sub ExtractMapData(tbl As ODTable)

    Dim ent As AcadEntity

    '' loop through each entity
    For Each ent In ThisDrawing.ModelSpace
    If Typeof ent Is AcadLWPolyline Then GetObjectData ent, tbl
    Next

    End Sub

    Private Sub GetObjectData(ent As AcadEntity, tbl As AutocadMAP.ODTable)

    Dim records As ODRecords
    Dim var As Variant

    Set records = tbl.GetODRecords()

    If records.Init(ent, False, True) Then

    Do Until records.IsDone

    ''Get data filed by field and save to file/database

    ''' If I comment out folowing lines of code, the memory usage
    during processing changes a bit, by increasing 5 to 10 MB
    ''' if these lines of code were executed, memory usage increases
    quickly, leading to crash.
    ''' it seems, if I access object data by using
    records.Record.Item(index), the object data were get into memory to be
    pointed to
    ''' but is not released, even var is the only variable
    referenced to it and goes out scope

    ''' var = records.record.Item(0).Value
    ''' var = records.record.Item(1).Value
    ''' var = records.record.Item(2).Value
    ''' var = records.record.Item(3).Value
    ''' var = records.record.Item(4).Value
    ''' var = records.record.Item(5).Value
    ''' var = records.record.Item(6).Value
    ''' var = records.record.Item(7).Value
    ''' var = records.record.Item(8).Value
    ''' .....
    ''' var = records.record.Item(40).Value

    records.Next

    Loop

    End If

    Set records=Nothing ''' Not necessary, but no harm and does not solve
    the problem

    End Sub


    Does anyone have similar problem?

    Previusly, I had trouble with ODRecords.Reocrd.Item(index).Value. for
    example, when you set a object data filed value of text type:

    Dim strValue as string 'strValue should be ""
    records.Record.Item(index).Value=strValue ''' Crashes!!!

    if yo do

    Dim strValue as string
    strValue=""
    records.Record.Item(index)=strValue ''' OK!!!, you just expliciltly
    assign strValue to "",
    ''' while we
    all know VB/VBA make "" as default string value when string variable is
    declared

    This example may indicate some weired COM interface impliment of AcadMAP
    object data. I hope someone could point out it is my mistake that causes the
    serious memory leak
     
    Norman Yuan, Feb 16, 2005
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.