Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: TV3D crashing (attempted to read or write protected memory)  (Read 4750 times)
m00se
Community Member
*
Posts: 3


« on: March 22, 2011, 11:00:46 AM »

Hi everyone,

Using:
VB.NET 2010 Express
MTV3D65
.NET 3.5

My program has to display some basic meshes (only a few dozen of boxes and spheres) in a picturebox within an MDIchild-form. The scene needs to be redrawn when I change something, like adding a sphere. It always starts ok, but after about a minute it crashes with this nice error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The basic structure of the code of the MDIchild:
Quote
Imports MTV3D65

Public Class View3DForm

    Private TV As TVEngine
    '(...)

    Public Sub View3DForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        '(Gather some data from the main form etc.)

        StartScene()
    End Sub

   Private Sub SetupScene(ByVal hWnd As IntPtr)
   
        bLoop = True
        TV = New TVEngine
        Scene = New TVScene
        Input = New TVInputEngine
        Cam = New TVCamera
        Mats = New TVMaterialFactory
        Texs = New TVTextureFactory
        Maths = New TVMathLibrary
        Light = New TVLightEngine

        TV.DisplayFPS(False)
        TV.Init3DWindowed(Me.pic.Handle.ToInt32, True)
        TV.SetAngleSystem(CONST_TV_ANGLE.TV_ANGLE_DEGREE)
        Input.Initialize(True, True)

        Light.CreatePointLight(New TV_3DVECTOR(300, 300, 300), 1, 1, 1, 1000)

        Light.EnableLight(IDLight, True)
        Light.SetSpecularLighting(True)

        For i = 0 To PlayerCheckListBox.Items.Count - 1 '(A list containing meshes; don't bother the arrays hereunder)
            If PlayerCheckListBox.CheckedIndices.Contains(i) Then

                Dim PlayerMesh As TVMesh
                PlayerMesh = Scene.CreateMeshBuilder("Mesh")
                PlayerMesh.CreateSphere(radarr(i), 100, 100)
                PlayerMesh.SetScale(weiarr(i, 0), weiarr(i, 1), weiarr(i, 2))
                PlayerMesh.SetPosition(posarr(i, 0), posarr(i, 1), posarr(i, 2))

                IDTex = Texs.CreateTexture(1, 1, False)
                Texs.SetPixel(IDTex, 0, 0, -PlayerForm.Grid.Rows(i).Cells(3).Value)

                PlayerMesh.SetMaterial(IDMat)
                PlayerMesh.SetTexture(IDTex)
                PlayerMesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_OFFSETBUMPMAPPING_TANGENTSPACE)

            End If
        Next

    End Sub

    Private Sub Main_Loop()
        While bLoop = True
            fTime = TV.AccurateTimeElapsed()
            TV.Clear()
            Scene.RenderAllMeshes()
            TV.RenderToScreen()
            UpdateScene()
            Application.DoEvents()
        End While
        TV.ReleaseAll()
        TV = Nothing
    End Sub

    Private Sub UpdateScene()

        Dim vNew As TV_3DVECTOR = Maths.MoveAroundPoint(New TV_3DVECTOR(0, 0, 0), fx, fy, fz)
        Cam.SetCamera(vNew.x, vNew.y, vNew.z, 0, 0, 0)
       
        '(Camera movement)

    End Sub

    Private Sub StartScene()
        pic.Focus() '(pic contains the TV3D image)
        PlayerCheckListBox.ClearSelected()
        Try
            TV.ReleaseAll()
            TV = Nothing
        Catch
        End Try
        bLoop = False
        Try
            TV.ReleaseAll()
            TV = Nothing
        Catch
        End Try
        SetupScene(Me.Handle)
        Main_Loop()
    End Sub

    Private Sub PlayerCheckListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayerCheckListBox.SelectedIndexChanged
        StartScene()
        '(So when I check an item in the listbox, the corresponding mesh is added)
    End Sub

End Class

Thanks in advance.
Logged
Mithrandir
Community Member
*
Posts: 326


« Reply #1 on: March 22, 2011, 06:13:43 PM »

Quote
Code:
Texs.SetPixel(IDTex, 0, 0, -PlayerForm.Grid.Rows(i).Cells(3).Value)

You have to lock the texture before changing it, and unlock it afterwards.

And I assume you release the TV class when opening new MDI window. This will destroy all your meshes, textures, ... in your other windows and generally causes problems (correct me if I'm wrong).
You should create just one instance of TVEngine (and other classes too) and dont destroy/release them until your program ends. Most of them are singletons which means that when you call "new TVEngine" you always get reference to the same instance.
If you want to render separate scenes you have to manage these scenes yourself: enable/disable meshes, reset cameras before each render, etc.
Logged
m00se
Community Member
*
Posts: 3


« Reply #2 on: March 23, 2011, 02:00:08 AM »

Thanks Mithrandir, I'll try that.
Logged
m00se
Community Member
*
Posts: 3


« Reply #3 on: March 24, 2011, 03:40:18 AM »

Maybe I wasn't clear: the MDIchild is the only form containing TV3D; the other forms only contain 'normal stuff'.
Logged
tanoshimi
Community Member
*
Posts: 116


« Reply #4 on: May 09, 2012, 12:40:19 PM »

I believe I have discovered the issue here.  I was suffering the same problem, but not on an MDI child.  I found that I can run TV fine on a form or in a PictureBox.  But if I place any Windows controls on the same form, TV does not initialize properly.  That caused my Scene object to not be initialized properly, and calls to CreateActor and CreateMeshBuilder would fail with the dreaded "Attempted to access protected memory."  Once I removed all controls from the form, it works fine.  This has been completely reproducible.  I'd assume, even if there are no controls on the MDIChild form, the MDIChild itself is the problem, but it could be a menu item or toolbar killing it.  This has only happened for me since I upgraded from VS 2008 to VS 2010, and I'm using exclusively VB.NET.  Hope this helps.  Maybe Sylvain can identify the problem.

Edit:

I moved my preview screen to a separate form with no controls on it.  It opens fine, but when the main form displays (it's the one with controls on it), the preview window locks up and I get the following error:
Quote
A first chance exception of type 'System.AccessViolationException' occurred in MTV3D65.dll

So apparently TV cannot exist in any project that also shows controls, using VB.NET in VS2010.
« Last Edit: May 09, 2012, 03:28:24 PM by tanoshimi » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks