MantisBT - Community
View Issue Details
0030516Community[OCCT] OCCT:Visualizationpublic2019-02-23 05:432019-05-19 10:54
daidalos77 
bugmaster 
normalcrash 
verifiedfixed 
WindowsVC++ 201564 bit
[OCCT] 6.8.0 
[OCCT] 7.4.0 
Not required
0030516: Visualization - Pointer to an OpenGl_Structure is deleted and accessed later after PrsMgr_Presentation::Highlight()
When creating a 3d shape(g.e. box) and then displaying it and then shading and then hilighting it and last trying to remove it from every viewer a deleted OpenGl_Structure pointer fails to be removed from OpenGl_BVHClipPrimitiveSet::myStructs
#include <AIS_Trihedron.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <Geom_Axis2Placement.hxx>
#include <AIS_InteractiveContext.hxx>
#include <AIS_Shape.hxx>
#include <OpenGl_GraphicDriver.hxx>
#include <V3d_View.hxx>
#include <V3d_Viewer.hxx>
#include <WNT_Window.hxx>


#pragma comment (lib , "TKernel.lib")
#pragma comment (lib , "TKG3d.lib")
#pragma comment (lib , "TKMath.lib")
#pragma comment (lib , "TKOpenGl.lib")
#pragma comment (lib , "TKPrim.lib")
#pragma comment (lib , "TKService.lib")
#pragma comment (lib , "TKTopAlgo.lib")
#pragma comment (lib , "TKV3d.lib")

Handle( V3d_Viewer ) myViewerEx;
void TestOpenCascade( HWND wnd )
{
    Handle( Graphic3d_GraphicDriver ) defaultGraphicDriver = new OpenGl_GraphicDriver( new Aspect_DisplayConnection() );
    Handle( V3d_Viewer ) myViewer = new V3d_Viewer( defaultGraphicDriver );
    myViewerEx = myViewer;
    Handle( AIS_InteractiveContext ) myContext = new AIS_InteractiveContext( myViewer );
    Handle( V3d_View ) myView = myViewer->CreateView();
    Handle( WNT_Window ) hWNTWnd = new WNT_Window( reinterpret_cast<HWND>(wnd) );
    myView->SetWindow( hWNTWnd );

    myView->SetAutoZFitMode( Standard_True );
    myView->MustBeResized();
    
    Handle( AIS_Trihedron ) myTrihedron = new AIS_Trihedron( new Geom_Axis2Placement( gp::XOY() ) );
    
    myContext->Display( myTrihedron, Standard_False );
    myContext->Deactivate( myTrihedron );//Deactivate selection of trihedron

    TopoDS_Shape aBoxShape = BRepPrimAPI_MakeBox( 100, 100, 100 );
    Handle( AIS_Shape ) myAIS = new AIS_Shape( aBoxShape );
    myContext->Display( myAIS, Standard_True );
    myContext->SetDisplayMode( myAIS, AIS_Shaded, Standard_True );
    myContext->HilightWithColor( myAIS, myContext->HighlightStyle( Prs3d_TypeOfHighlight_Dynamic ), Standard_True );
    myContext->Remove( myAIS, Standard_True );
};
Added trace function to following functions


void trace( const char *format, ... )
{
    va_list args;
    va_start( args, format );

    const int cChars = vsnprintf( NULL, 0, format, args );
    char *pBuffer = new char[cChars + 1]{ '\0' };
    vsnprintf( pBuffer, cChars + 1, format, args );
    ::OutputDebugStringA( pBuffer );
    delete[] pBuffer;
    va_end( args );
}
unsigned int deletemeplsnow = 0, deletemeplsnow2 = 0, deletemeplsnow3 = 0;

OpenGl_Structure::~OpenGl_Structure()
{
  trace( "OpenGl_Structure::~OpenGl_Structure %p\r\n", this );
  Release (Handle(OpenGl_Context)());
}

Graphic3d_BndBox3d OpenGl_BVHClipPrimitiveSet::Box (const Standard_Integer theIdx) const
{
    trace( "OpenGl_BVHClipPrimitiveSet::Box(%d) : %p\r\n", deletemeplsnow2++, myStructs.FindKey( theIdx + 1 ) );
  return myStructs.FindKey (theIdx + 1)->BoundingBox();
}

Standard_Boolean OpenGl_BVHClipPrimitiveSet::Add (const OpenGl_Structure* theStruct)
{
  const Standard_Integer aSize = myStructs.Size();
  trace( "OpenGl_BVHClipPrimitiveSet::Add(%d) : %p\r\n", deletemeplsnow++, theStruct );
  if (myStructs.Add (theStruct) > aSize) // new structure?
  {
    MarkDirty();

    return Standard_True;
  }

  return Standard_False;
}

Standard_Boolean OpenGl_BVHClipPrimitiveSet::Remove (const OpenGl_Structure* theStruct)
{
    trace( "OpenGl_BVHClipPrimitiveSet::Remove(%d) : %p\r\n", deletemeplsnow3++, theStruct );
  const Standard_Integer anIndex = myStructs.FindIndex (theStruct);

  if (anIndex != 0)
  {
    myStructs.Swap (Size(), anIndex);
    myStructs.RemoveLast();
    MarkDirty();

    return Standard_True;
  }

  return Standard_False;
}





RESULT OUTPUT:

OpenGl_BVHClipPrimitiveSet::Add(0) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Add(1) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Box(0) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Add(2) : 0000004F60AACFA0
OpenGl_BVHClipPrimitiveSet::Add(3) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Add(4) : 0000004F60AACFA0
OpenGl_BVHClipPrimitiveSet::Box(1) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Box(2) : 0000004F60AACFA0
OpenGl_BVHClipPrimitiveSet::Box(3) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Box(4) : 0000004F60AACFA0
OpenGl_BVHClipPrimitiveSet::Box(5) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Box(6) : 0000004F60AACFA0
OpenGl_BVHClipPrimitiveSet::Box(7) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Box(8) : 0000004F60AACFA0
OpenGl_BVHClipPrimitiveSet::Box(9) : 0000004F60AACB50
OpenGl_BVHClipPrimitiveSet::Box(10) : 0000004F60AACFA0
OpenGl_Structure::~OpenGl_Structure 0000004F60AACB50 <------deleted
OpenGl_BVHClipPrimitiveSet::Remove(0) : 0000004F60AACFA0
OpenGl_Structure::~OpenGl_Structure 0000004F60AACFA0
OpenGl_BVHClipPrimitiveSet::Box(11) : 0000004F60AACB50 <------accessed
OpenGl_Structure::~OpenGl_Structure 0000004F60AACE30


QUICK AND DIRTY SOLUTION FOR MY CASE JUST BEFORE CALLING REMOVE FUNCTION:


    PrsMgr_Presentations &shapePresentations = myAIS->Presentations();
    for( int i = 0; i < shapePresentations.Length(); ++i )
        shapePresentations.Value( i + 1 ).Presentation()->Presentation()->SetIsForHighlight( false );


myContext->Remove( myAIS, Standard_True );


RESULT OUTPUT:

OpenGl_BVHClipPrimitiveSet::Add(0) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Add(1) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Box(0) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Add(2) : 000000F030FFD5E0
OpenGl_BVHClipPrimitiveSet::Add(3) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Add(4) : 000000F030FFD5E0
OpenGl_BVHClipPrimitiveSet::Box(1) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Box(2) : 000000F030FFD5E0
OpenGl_BVHClipPrimitiveSet::Box(3) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Box(4) : 000000F030FFD5E0
OpenGl_BVHClipPrimitiveSet::Box(5) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Box(6) : 000000F030FFD5E0
OpenGl_BVHClipPrimitiveSet::Box(7) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Box(8) : 000000F030FFD5E0
OpenGl_BVHClipPrimitiveSet::Box(9) : 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Box(10) : 000000F030FFD5E0
OpenGl_BVHClipPrimitiveSet::Remove(0) : 000000F0310376F0
OpenGl_Structure::~OpenGl_Structure 000000F0310376F0
OpenGl_BVHClipPrimitiveSet::Remove(1) : 000000F030FFD5E0
OpenGl_Structure::~OpenGl_Structure 000000F030FFD5E0
OpenGl_Structure::~OpenGl_Structure 000000F030FACB40
No tags attached.
related to 0030491new nds Open CASCADE Visualization - OpenGl_Element inherits Standard_Transient 
related to 0024307closed apn Open CASCADE TKOpenGl - efficient culling of large number of presentations 
Issue History
2019-02-23 05:43daidalos77New Issue
2019-02-23 05:43daidalos77Assigned To => kgv
2019-02-23 13:35kgvRelationship addedrelated to 0030491
2019-02-23 13:35kgvTarget Version => 7.4.0
2019-02-23 13:37kgvRelationship addedrelated to 0024307
2019-03-14 18:16kgvAssigned Tokgv => tizmaylo
2019-03-14 18:16kgvStatusnew => assigned
2019-03-20 11:49gitNote Added: 0083132
2019-03-20 11:56tizmayloAssigned Totizmaylo => kgv
2019-03-20 11:56tizmayloStatusassigned => resolved
2019-03-20 11:56tizmayloSteps to Reproduce Updatedbug_revision_view_page.php?rev_id=20883#r20883
2019-03-20 12:05tizmayloAssigned Tokgv => tizmaylo
2019-03-20 12:06tizmayloSteps to Reproduce Updatedbug_revision_view_page.php?rev_id=20884#r20884
2019-03-20 12:09tizmayloAssigned Totizmaylo => kgv
2019-03-21 14:35kgvSummaryPointer to an OpenGl_Structure is deleted and accessed later => Visualization - Pointer to an OpenGl_Structure is deleted and accessed later
2019-05-02 17:02kgvProduct Version7.2.0 => 6.8.0
2019-05-02 17:03kgvStatusresolved => assigned
2019-05-02 17:03kgvSummaryVisualization - Pointer to an OpenGl_Structure is deleted and accessed later => Visualization - Pointer to an OpenGl_Structure is deleted and accessed later after PrsMgr_Presentation::Highlight()
2019-05-02 17:06gitNote Added: 0084103
2019-05-02 17:21kgvNote Added: 0084104
2019-05-02 19:19kgvNote Added: 0084105
2019-05-02 19:19kgvAssigned Tokgv => san
2019-05-02 19:19kgvStatusassigned => resolved
2019-05-07 09:16kgvAssigned Tosan => osa
2019-05-08 10:50osaNote Added: 0084237
2019-05-08 10:50osaAssigned Toosa => bugmaster
2019-05-08 10:50osaStatusresolved => reviewed
2019-05-15 12:00bugmasterNote Added: 0084359
2019-05-15 12:00bugmasterStatusreviewed => tested
2019-05-15 12:01bugmasterTest case number => Not required
2019-05-19 10:40bugmasterChangeset attached => occt master 49dfdb7a
2019-05-19 10:40bugmasterStatustested => verified
2019-05-19 10:40bugmasterResolutionopen => fixed
2019-05-19 10:54gitNote Added: 0084451
2019-05-19 10:54gitNote Added: 0084454

Notes
(0083132)
git   
2019-03-20 11:49   
Branch CR30516 has been created by tizmaylo.

SHA-1: cfd94674d3017fbd9b169b13752270f331f31412


Detailed log of new commits:

Author: tizmaylo
Date: Wed Mar 20 11:40:30 2019 +0300

    0030516: Pointer to an OpenGl_Structure is deleted and accessed later
    
    The flag OpenGl_Structure::IsForHighlight is set to false before erasing of an OpenGl_Structure entity to prevent occurence of dead pointers to it in OpenGl_Layer::myBVHPrimitives.
(0084103)
git   
2019-05-02 17:06   
Branch CR30516_1 has been created by kgv.

SHA-1: c4a638db268d4232e94d0d6f43723a23a5562ca5


Detailed log of new commits:

Author: kgv
Date: Thu May 2 16:58:06 2019 +0300

    0030516: Visualization - Pointer to an OpenGl_Structure is deleted and accessed later after PrsMgr_Presentation::Highlight()
    
    Do not set IsForHighlight flag for already displayed presentation.
(0084104)
kgv   
2019-05-02 17:21   
The problem cause is PrsMgr_Presentation::Highlight() -> PrsMgr_Presentation::display(true) setting IsForHighlight flag for already displayed (but hidden) Structure, which makes no sense. In the following script Wireframe presentation is hidden (but already in BVH tree for frustum culling, hence no use to remove it from there) and revealed by selection:
pload MODELING VISUALIZATION
box b 1 2 3
vclear
vinit View1
vdisplay b
vfit
vsetdispmode b 1
vmoveto 250 250
vselect 250 250


There is a general issue though - when highlighted structure was not displayed, IsForHighlight flag results in no frustum culling for it. Consider selection of entire scene with local selection turned ON (which is unfortunate scenario anyway), it will lead to a lot of structures being not frustum culled. This is a subject for another issue though:
pload MODELING VISUALIZATION
box b 1 2 3
vclear
vinit View1
vdisplay b
vfit
vsetdispmode b 1
vselmode b FACE 1
vmoveto 250 250
vselect 250 250
(0084105)
kgv   
2019-05-02 19:19   
Patch is ready for review.

http://jenkins-test-12.nnov.opencascade.com:8080/view/CR30516_1-master-KGV/ [^]
(0084237)
osa   
2019-05-08 10:50   
Patch CR30516_1 was reviewed without remarks
(0084359)
bugmaster   
2019-05-15 12:00   
Fix has been tested in framework of WEEK-20 branch

Combination -
OCCT branch : WEEK-20
master SHA - 853af31fb14e2140d821c7ba1c5c5977072d7662
d67d4b811012eef8913d3c535c29654d0acf3c4c
Products branch : WEEK-20 SHA - bde66da19002bfd6a68440a079d00728c5796b30
was compiled on Linux, MacOS and Windows platforms and tested in optimize mode.

Number of compiler warnings:
No new/fixed warnings

Regressions/Differences/Improvements:
No regressions/differences

CPU differences:
Debian80-64:
OCCT
Total CPU difference: 16362.230000000052 / 16346.709999999983 [+0.09%]
Products
Total CPU difference: 10506.300000000043 / 10516.200000000064 [-0.09%]
Windows-64-VC14:
OCCT
Total CPU difference: 17708.015625 / 17692.25 [+0.09%]
Products
Total CPU difference: 12195.578125 / 12107.796875 [+0.72%]


Image differences :
No differences that require special attention

Memory differences :
No differences that require special attention
(0084451)
git   
2019-05-19 10:54   
Branch CR30516_1 has been deleted by inv.

SHA-1: c4a638db268d4232e94d0d6f43723a23a5562ca5
(0084454)
git   
2019-05-19 10:54   
Branch CR30516 has been deleted by inv.

SHA-1: cfd94674d3017fbd9b169b13752270f331f31412