Memory Leak?

Memory Leak?

andypugh
Enthusiast Enthusiast
760 Views
10 Replies
Message 1 of 11

Memory Leak?

andypugh
Enthusiast
Enthusiast

I have a macro that creates an SVG file for a DLP printer from an Inventor part. 

 

I have asked a different question about this project before, related to converting splines to Beziers, and got a lot of help here for which I am grateful. 

 

The macro does now work (though I am not 100% happy with converting splines to short line segments, and still hope to find a clevere solution). 

 

However, it eats memory. I don't see any reason for this, and I have added Erase and =Nothing code to try to prevent this, but it still eats all 6GB eventually. 

 

Attached is the .ivb file. If you wish to test it then at the moment it needs to be called from the Macro menu.

 

Click "start plane" then click a part face, set the slice thickness to 5um, and watch memory slowly disappear in the Windows Resource Monitor. 

 

The main loop is in the ClsToolBarEvents class module in the "SliceIt" method, with calls out to the SVGFile Class. 

 

I think I have over-done my garbage collection, as I think that most of the objects I create go out of scope automatically. It would be almost as useful to know which destructors are superflous as to know where my missing destrector and memory leak is. 

 

I am, of course, assuming that this is my fault. Is it possible that the memory leak is in AutoDesk code? 

0 Likes
761 Views
10 Replies
Replies (10)
Message 2 of 11

andypugh
Enthusiast
Enthusiast
I think I know how to proceed on this, I will begin by commenting-out the function calls in the loop until the problem goes away, and track down the offending code or external call that way.
Once I have tracked it down I will post a minimal demo routine if I can't solve the problem.
0 Likes
Message 3 of 11

andypugh
Enthusiast
Enthusiast

I have stripped down the code considerably, and the memory usage still increases steadily until the system slows to a crawl. 

 

The code offsets a work plane through an ipart in 5um steps, creating a cut profile in a sketch at each position. 

 

In the "Real" code this is converted to an outline in an SVG file, but in this example the sketch is simply deleted and the next slice taken. 

 

If you take a random part and create a workplane somwhere on/in the part and run the code you should see the Inventor memory usage monotonically increasing as the slicing proceeds. 

 

It seems to me like oSketch.Delete is not deleting all the contents of the sketch. Perhaps I need to delete the Profiles separately? 

 

Sub slicetest()

Dim oDef As PartComponentDefinition
Set oDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oSketch As PlanarSketch

Dim nWP As WorkPlane

Dim offset As Double, delta As Double
offset = 0
delta = 0.0005

'Plane 4 is the first non-fixed workplane
Set nWP = oDef.WorkPlanes.AddByPlaneAndOffset(oDef.WorkPlanes(4), delta)
Set oSketch = oDef.Sketches.Add(nWP, False)

'check the extrude direction
On Error Resume Next
oSketch.ProjectedCuts.Add
If Err <> 0 Then
    delta = -delta
End If
oSketch.Delete

'This seems to be where the memory-leak is

While 1
    nWP.SetByPlaneAndOffset oDef.WorkPlanes(4), offset
    Set oSketch = oDef.Sketches.Add(nWP, False)
    On Error Resume Next
        oSketch.ProjectedCuts.Add
        oSketch.Profiles.AddForSolid
        If Err <> 0 Then
            Exit Sub
        End If
    On Error GoTo 0
    DoEvents
    oSketch.Delete
    Set oSketch = Nothing
    offset = offset + delta
Wend

End Sub

 

0 Likes
Message 4 of 11

andypugh
Enthusiast
Enthusiast

I don't know if anyone is interested, but this didn't help...

 

    While oSketch.Dependents.count > 0
        oSketch.Dependents.Item(1).Delete
    Wend

 

0 Likes
Message 5 of 11

andypugh
Enthusiast
Enthusiast

Commenting out 

        'oSketch.ProjectedCuts.Add

 Makes the problem go away. But that also means that the routine doesn't do what it is meant to do. 

 

So, the question is how to clean-up the Projected cuts before deleting the sketch (as deleting the sketch doe snot sem to clean up properly) Simply deleting them in a whole loop does not seem to do the trick. 

 

Any ideas? 

 

 

0 Likes
Message 6 of 11

Vladimir.Ananyev
Alumni
Alumni

Could you try move the existing work plane changing the offset parameter value instead of creating every time the new one by AddByPlaneAndOffset method?  I didn’t test it with your code, just an idea.
cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 7 of 11

andypugh
Enthusiast
Enthusiast

The cut-down sample code in the third post does take the route of moving an existing workplane, and that makes no difference.

 

I have (by commenting out lines) isolated the issue to oSketch.projectedCuts.Add (5th post in the thread). And from that point I don't see a way to proceed.

0 Likes
Message 8 of 11

Vladimir.Ananyev
Alumni
Alumni

I could reproduce this effect on my side, so  I've logged the change request. 
Thank you very much for all your efforts !

Best regards,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 9 of 11

andypugh
Enthusiast
Enthusiast

Thanks for investigating. It is a pity that there isn't an easy solution, but for the moment the Interim Containing Action is 16GB of RAM. 

0 Likes
Message 10 of 11

Vladimir.Ananyev
Alumni
Alumni

As a temporary workaround you may interrupt your slicing process after the specified number of iterations and close the document or execute “Close All” command to release memory, then reload the model and restart calculations from the next height level.

Here is my result when I closed the current document two times:

Close.PNG

80000 iterations before close document operation.  
Hope this is applicable in your workflow. 

I used the following slightly modified slicing cycle:

```````````````````````````````````````````````    
    On Error Resume Next
    While 1
        wp.Definition.offset.value = offset
        
        Set oSketch = oDef.Sketches.Add(wp, False)
        oSketch.ProjectedCuts.Add      
        oSketch.Profiles.AddForSolid
        
        If Err <> 0 Then
            Err.Clear
            oSketch.Delete
            Set oSketch = Nothing
            GoTo finish
        End If
        DoEvents
        
        oSketch.Delete
        Set oSketch = Nothing
        offset = offset + delta
    Wend
    
finish:
    On Error GoTo 0
    wp.Delete
    Beep
End Sub

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 11 of 11

andypugh
Enthusiast
Enthusiast

"

80000 iterations before close document operation.  
Hope this is applicable in your workflow. "

 

I will look into it. It might be a little diffficult as the slicing starts from user-selected geometry and it sems likely that the reference would be lost during a CloseAll.

0 Likes