Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] AccessViolationException when using RenderWindow in C++CLI  (Read 4291 times)

0 Members and 1 Guest are viewing this topic.

ThreeDumps

  • Newbie
  • *
  • Posts: 34
    • View Profile
Hello. I have managed class MainApp. Managed class can have only other managed types:
Quote
cannot define 'something' as a member of managed 'projekt_name': mixed types are not supported

I have other class (lets call it B) (this class have sf::RenderWindow) and it is normal C++ class. MainApp need to contain B. I can't have B in managed class so i have B* and in MainApp construtor i create new object for this pointer. In B i construct sf::RenderWindow by RenderWindow (WindowHandle handle), handle come from MainApp.

In B construktor I set setMouseCursorVisible( false ); and everything is ok so far.

But when I use display() or clear() evrything is going good but! When I close application i have
Quote
An unhandled exception of type 'System.AccessViolationException' occurred in project_name

I first destroy class B, and after that i destroy rest of controls in MainApp (when I change order effect is that same).

What prabobly I made wrong? It's banned to use sf::RenderWindow in object made by new?
« Last Edit: August 21, 2013, 10:20:34 pm by akwes »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: AccessViolationException when using RenderWindow in C++CLI
« Reply #1 on: August 21, 2013, 05:49:33 pm »
Sounds like your destructors are not working properly. You should post a complete and minimal example that causes this.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

ThreeDumps

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: AccessViolationException when using RenderWindow in C++CLI
« Reply #2 on: August 21, 2013, 07:36:45 pm »
I had hope that was fundamental problem. Well... Here is code:

B.h
#pragma once
#include <SFML/Graphics.hpp>
#include <Windows.h>

class B {
        sf::RenderWindow app;
public:
        B( HWND windowHandle ) : app(windowHandle) { }
        void draw() {
                app.clear( sf::Color::White );
                app.display();
        }
};
 

mainApp.h
#pragma once
#include "B.h"

namespace Project {

        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;

        /// <summary>
        /// Summary for mainApp
        /// </summary>
        public ref class mainApp : public System::Windows::Forms::Form
        {
                B * b;  
        public:
                mainApp(void)
                {
                        InitializeComponent();
                        b = new B( static_cast<HWND>(panel3->Handle.ToPointer()) );
                }

                void draw() {      
                        b->draw();  
                }                        
        protected:
                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                ~mainApp()
                {
                        delete b;    
                        if (components)
                        {
                                delete components;
                        }
                }
        private: System::Windows::Forms::Panel^  panel3;


        private:
                /// <summary>
                /// Required designer variable.
                /// </summary>
                System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                void InitializeComponent(void)
                {
                        System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(mainApp::typeid));
                        this->panel3 = (gcnew System::Windows::Forms::Panel());
                        this->SuspendLayout();
                        //
                        // panel3
                        //
                        this->panel3->Dock = System::Windows::Forms::DockStyle::Fill;
                        this->panel3->Location = System::Drawing::Point(3, 16);
                        this->panel3->Name = L"panel3";
                        this->panel3->Size = System::Drawing::Size(160, 256);
                        this->panel3->TabIndex = 1;
                        //
                        // mainApp
                        //
                        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                        this->ClientSize = System::Drawing::Size(948, 566);
                        this->Name = L"mainApp";
                        this->Text = L"mainApp";
                        this->ResumeLayout(false);
                        this->PerformLayout();

                }
#pragma endregion
        };
}
 

main.cpp
#include "mainApp.h"
using namespace System;

[STAThreadAttribute]
int main()
{
        Project::mainApp mainApp;
        mainApp.Show();

        while (true) {
                System::Windows::Forms::Application::DoEvents();
                mainApp.draw();
        }
    return 0;
}
 

mainApp.h have some code, but most of it is auto generated.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: AccessViolationException when using RenderWindow in C++CLI
« Reply #3 on: August 21, 2013, 07:45:42 pm »
Blind guess: Maybe give B a destructor that does app.close()?

ThreeDumps

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: AccessViolationException when using RenderWindow in C++CLI
« Reply #4 on: August 21, 2013, 07:56:55 pm »
@Ixrec, nope. Still same.

class B {
        sf::RenderWindow app;
public:
        B( HWND windowHandle ) : app(windowHandle) { }
        void draw() {
                app.clear( sf::Color::White );
                app.display();
        }
        ~B() {
                app.close();
        }
};


zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: AccessViolationException when using RenderWindow in C++CLI
« Reply #5 on: August 21, 2013, 08:09:23 pm »
Without being able to test anything, how about changing

   ~mainApp()
        {
            delete b;    
            if (components)
            {
                delete components;
            }
        }
while (true) {
        System::Windows::Forms::Application::DoEvents();
        mainApp.draw();
    }

to something like

   ~mainApp()
        {
            if (b) delete b;    
            if (components)
            {
                delete components;
            }
        }
while (mainApp.IsOpen()) {
        System::Windows::Forms::Application::DoEvents();
        mainApp.draw();
    }
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

ThreeDumps

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: AccessViolationException when using RenderWindow in C++CLI
« Reply #6 on: August 21, 2013, 08:48:59 pm »
@zsbzsb, hit!

while (true) {
        System::Windows::Forms::Application::DoEvents();
        mainApp.draw();
}
 

This loop is still working after mainApp destruction.

Don't know what to do with this topic. Problem solution have nothing common with SFML :) Well, thank you all for help.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: AccessViolationException when using RenderWindow in C++CLI
« Reply #7 on: August 21, 2013, 08:53:36 pm »
Quote
Don't know what to do with this topic. Problem solution have nothing common with SFML :) Well, thank you all for help.

Just let it sink into the depths of the forums.... And you could also modify to title and add "[Solved]".  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything