Removing Interactive Objects from Interactive Context

Hi everybody,

I'm having a problem removing objects from my AIS_InteractiveContext. Within my application I load a BRep file and create an AIS_Shape object like this:

Handle(AIS_Shape) myShapeHandle = new AIS_Shape(myTopoDS_Shape);

Reference Counter is now 1;

Afterwards I display the AIS_Shape using AIS_InteractiveContext::Display(myShapeHandle);

Reference Counter is now 3;

The handle is stored in a class. When I want to change the BRep file I have to remove the AIS_Shape first from the viewer. I'm doing this using AIS_InteractiveContext::Remove(myShapeHandle);

Reference Counter decreases to 2 ==> Should decrease to 1, but doesn't!!! Why???

Afterwards I Nullify the handle to release the memory of the AIS_Shape object.


Nullify decreases the Reference Counter first by one and then checks whether the counter is 0. If so the delete operator of AIS_Shape is called, to release its memory.

As the Remove() function does (That's my problem!!!) decrease the Reference Counter only by 1 and not by 2. The memory is not released and I get a memory leak if I read a new BRep file and assign the new AIS_Shape object to the same handle.

Another OCC user already has reported to me that when he uses the Remove() function the corresponding reference counter is decreased correctly by 2 from 3 to 1. So I wonder why within my application I am having this problem.

Does anyone have a clue what could be wrong?

Thanks in advance and best regards.

Sven Jungnickel

Neil's picture

Hi Sven,

One thing I noticed from your code is that you are creating a handle to the shape when you call the constructor:

Handle(AIS_Shape) myShapeHandle = new AIS_Shape(myTopoDS_Shape);

whereas because you are storing the handle in a class, I would have expected to see:

m_myShapeHandle = new AIS_Shape(myTopoDS_Shape)

and a appropriate member declared in the header for your class:

Handle(AIS_Shape) m_myShapeHandle;

Is it possible that you have two handles pointing at the same AIS_Shape, so the reference counter is increased by 1? Although in this case, I would have expected the reference counter to increase to 4 when you display the shape, so I doubt this is the problem.

Sorry if this is a stupid suggestion.



Sven Jungnickel's picture

Hi Neil.

thanks for your answer (once again). You are correct - but its Metacode. myShapeHandle is meant to be a member of some class.


n-southall's picture

Hi Sven,

Can you recreate the problem in a little test app? If so, I would be glad to have a look at it for you.



Sven Jungnickel's picture

Hi Neil,

I'm currently not in my office (until the middle of april). I'm writing this from home. Indeed building a small test app would be my next step. If I am able to recreate the problem I really would appreciate it if you could take a look at it. Perhaps I have a little spare time for this during my holidays. I'll contact again you again. Thank you so far for your help and interest in my problem.

Best regards,


Stephane Routelous's picture

Hi Sven,

can you try to reproduce it without selecting or hilighting the AIS_IO ( just display it and remove it ) ?
I already saw this problem a LONG time ago: ( should be fixed )
and ( still open )

Note to the OCC team : do you check sometimes the bugs entered in ? Are they just registered to the trash ?

Sven Jungnickel's picture

What is really funny is that I do nothing with the shape, just displaying it, do some panning, rotation, etc. Selection is disabled for this object all the time and it is definitly not highlighted by the user.

Thanks for your hint with the bug Stephane.

I think I'll make further investigation with version 5.1. Currently I'm running on 5.0


Francois Lauzon's picture

Hello Sven,
in fact, if you want to make sure you remove your ais from the context, you should use the method Clear instead of Remove.

Good Luck,

Sven Jungnickel's picture

Well I tried that before. Clear produces the same result.

The Erase() method with the PutInCollector Flag set to false should do (as I think) the same, but also produced the same result.


Sven Jungnickel's picture

The problem has been solved by upgrading from OCC Version 5.0 to 5.1. The function AIS_InteractiveContext::Remove() decreases the reference counter now correctly by 2, so that the memory can be released by calling the Nullify() method of the Handle(AIS_Shape) object.

Thanks for your support,


Sangsu Lee's picture

Hi, Sven.

Sometimes, AIS_InteractiveContext::Remove() is not sufficient to release all the memory related with a AIS_IO.

You may find the reference count of AIS_IO itself to be zero, but if you carefully investigate into the memory usage more deep, sometimes the memory used for presentations and selections is not released.

For this problem you should write one more code as below.

theContext->Remove( anAIS, Standard_False );
theContext->SelectionManager()->Remove( anAIS );

I heard that this problem will be fixed in the next OCC release.
But currently this will help you.

Sangsu Lee