locked
Why does reusing the points for a Polyline cause trouble?

    Question

  • I have a crash in a very simple Metro C++ program, and I have isolated it to a question of whether a PointCollection is reused or not.  Perhaps I don't understand how these ref new and ^ are supposed to work.

    It is just a BlankPage application with this added XAML in BlankPage.xaml

    <Grid x:Name="myGrid" Background="{StaticResource ApplicationPageBackgroundBrush}"> <StackPanel x:Name="myPanel" HorizontalAlignment="Center" VerticalAlignment="Center"> </StackPanel> </Grid>

    and then this code in Blankpage.xaml.cpp

    void BlankPage::OnNavigatedTo(NavigationEventArgs^ e) { auto points = ref new Windows::UI::Xaml::Media::PointCollection(); Glue::stairStep( points, 20, 8 ); auto otherPoints = ref new Windows::UI::Xaml::Media::PointCollection(); Glue::stairStep( otherPoints, 60, 4 ); Media::Brush^ brush= Glue::solidColorBrush( Windows::UI::Colors::MediumPurple ); bool causeTrouble= true; if ( causeTrouble ) { // Both lines won't execute together Glue::addLine( myPanel, points, brush, 8 ); Glue::addLine( myPanel, points, brush, 8 ); } else { Glue::addLine( myPanel, points, brush, 4 ); Glue::addLine( myPanel, otherPoints, brush, 4 ); } } 

    If causeTrouble is set true, I get

    // PolyTrouble.exe!<lambda_07be93b201170af5eae30a1f4f8ab9ac>::operator()(Platform::Object ^ sender, Windows::UI::Xaml::UnhandledExceptionEventArgs ^ e) Line 53 C++ // PolyTrouble.exe!`Windows::UI::Xaml::UnhandledExceptionEventHandler::UnhandledExceptionEventHandler<<lambda_07be93b201170af5eae30a1f4f8ab9ac> >'::`2'::__abi_FunctorCapture::Invoke(Platform::Object ^ __param0, Windows::UI::Xaml::UnhandledExceptionEventArgs ^ __param1) C++ // PolyTrouble.exe!Windows::UI::Xaml::UnhandledExceptionEventHandler::Invoke(Platform::Object ^ __param0, Windows::UI::Xaml::UnhandledExceptionEventArgs ^ __param1) C++ 

    If causeTrouble is false, then the code executes correctly. You can see the only difference between the two cases is whether the same data is being used for both lines, or not.  Reusing points causes the error.

    Perhaps there is something wrong in the parameter passing to addLine.  It is defined below:

    namespace Glue
    {
    	/// <summary>
    	/// Provides glue logic needed to do drawing with a Metro application.
    	/// Specifically Provides glue logic needed to do drawing with a Metro application.
    	/// </summary>
    
    
    Media::Brush^ textBrush()
    {
    	return dynamic_cast<Media::Brush^>( Windows::UI::Xaml::Application::Current->Resources->Lookup( "ApplicationTextBrush" ) );
    }
    
    Media::Brush^ solidColorBrush( Windows::UI::Color color )
    {
    	return ref new Windows::UI::Xaml::Media::SolidColorBrush(color);
    }
    
    
    Media::Transform ^ flipY()
    {
    	auto renderTransform = ref new Windows::UI::Xaml::Media::CompositeTransform();
    	renderTransform->ScaleY= -1;
    	return renderTransform;
    }
    
    // Draw an arbitrary Polyline of points onto the element using a ApplicationTextBrush of strokeThickness 3
    // which duplicates this XAML:
    /*
            <StackPanel x:Name="myCanvasPanel" HorizontalAlignment="Right" VerticalAlignment="Center">
                <Polyline x:Name="myLine" Stroke="{StaticResource ApplicationTextBrush}" StrokeThickness="3"  >
                    <Polyline.RenderTransform>
                        <CompositeTransform ScaleY="-1"/>
                    </Polyline.RenderTransform>
                </Polyline>
            </StackPanel> 
    
    */
    //
    void addLine( Windows::UI::Xaml::Controls::Panel^ element, Media::PointCollection^ points )
    {
    	auto dynPolyLine = ref new Windows::UI::Xaml::Shapes::Polyline();
    
    	dynPolyLine->Stroke= textBrush();
    	dynPolyLine->StrokeThickness= 3;
    	dynPolyLine->RenderTransform= flipY();
    
    	dynPolyLine->Points= points;
    
    	element->Children->Append( dynPolyLine );
    }
    
    
    // Draw an arbitrary Polyline of points onto the element using a brush of strokeThickness
    //
    void addLine( Windows::UI::Xaml::Controls::Panel^ element, Media::PointCollection^ points, Media::Brush^ brush, double strokeThickness )
    {
    	auto dynPolyLine = ref new Windows::UI::Xaml::Shapes::Polyline();
    
    	dynPolyLine->Stroke= brush;
    	dynPolyLine->StrokeThickness= strokeThickness;	// 3 for example
    	dynPolyLine->RenderTransform= flipY();
    
    	dynPolyLine->Points= points;
    
    	element->Children->Append( dynPolyLine );
    }
    
    
    void stairStep( Windows::UI::Xaml::Media::PointCollection^ points, int stepSize, int steps )
    {
    	float x= 0, y=0;
    	for ( int step=0; step < steps; step += 2 ) {
    		points->Append( Point( x, y ) );
    		x += stepSize;
    		points->Append( Point( x, y ) );
    		y += stepSize;
    	}
    
    }
    
    
    }
    

    Sunday, May 27, 2012 1:22 AM

All replies

  • Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 28, 2012 7:36 AM