Results 1 to 16 of 16

Thread: [RESOLVED] What could cause a running application to just close down ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [RESOLVED] What could cause a running application to just close down ?

    Hi,

    I have a little application which keeps closing down whilst I'm using it.

    I have no idea why it does that, it definitely closes down, it's not just hiding.
    There is an Exit button but the cursor can be nowhere near it when this happens.

    Just wondering what I should be looking for, or how to capture what causes it.
    Maybe there is something already implemented to do that which I'm not aware of ?

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: What could cause a running application to just close down ?

    Oh...

    Attempting to write to protected memory !

    What on earth is 'Protected memory' ?

    Code:
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim co, no, ro As Integer
    
            co = 5 : no = 1 : ro = 5
            While no < 226
                Dim box As New TextBox
                With box
                    .Name = "TextBox" & no.ToString
                    .AccessibleName = "TextBox" & no.ToString
                    .BackColor = Color.White
                    .BorderStyle = BorderStyle.FixedSingle
                    .Height = 40
                    .Width = 40
                    .Margin = New Padding(0, 0, 0, 0) '(Left, Top, Right, Bottom)
                    .Multiline = True
                    .Text = ""
                    .Location = New Point(co, ro)
                    .Visible = True
                    Me.Controls.Add(box)
                    AddHandler box.TextChanged, AddressOf BoxChange
                    AddHandler box.Click, AddressOf BoxClick
                    AddHandler box.MouseEnter, AddressOf BoxEnter
                    co += 40 : no += 1
                    If co > 570 Then co = 5 : ro += 40
                End With
            End While
        End Sub
    
        Private Sub BoxChange(sender As Object, e As System.EventArgs)
            Dim num As Int32, ch As Char, txt As String, box As TextBox = CType(sender, TextBox)
    
            If box.TextLength > 0 Then
                txt = box.Text : txt = txt.ToUpper : ch = txt.Last : num = Convert.ToInt32(ch)
                If num > 64 And num < 91 Then
                    box.Text = txt.ToUpper
                    box.Font = New Font("Comic Sans MS", 30, FontStyle.Regular, GraphicsUnit.Pixel)
                    box.TextAlign = HorizontalAlignment.Center
                Else
                    box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                End If
                box.Text = txt
            Else
                box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                box.TextAlign = HorizontalAlignment.Left
            End If
        End Sub
    How can it be that these textboxes are 'Protected' ?

    The problem occurs when I try to change an Alpha character in a textbox, I enter a new letter, the box goes blank, I move the cursor out of the box, application closes.


    Poppa
    Last edited by Poppa Mintin; Jul 2nd, 2022 at 09:03 AM. Reason: More info added.
    Along with the sunshine there has to be a little rain sometime.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: What could cause a running application to just close down ?

    AccessibleName ??? Use Name

  4. #4

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: What could cause a running application to just close down ?

    Thanks .Paul,

    Although it's coded, I'm not actually using AccessibleName anywhere, it's there just in case I need it.
    I took it out but it made no difference. I found 'Attempting to write to protected memory' after my original post but still haven't discovered (a) What causes it, or (b) What to do about it. I wonder if it has something to do with the fact that I'm using a Char in the code ?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  5. #5

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: What could cause a running application to just close down ?

    I've changed the code in Sub BoxChange to remove the Char but it's made no difference, the app. still just exits as previously described.

    Code:
        Private Sub BoxChange(sender As Object, e As System.EventArgs)
            Dim txt As String, box As TextBox = CType(sender, TextBox)
    
            If box.TextLength > 0 Then
                txt = box.Text.ToUpper : txt = txt.Last
                If txt >= "A" And txt <= "Z" Then
                    box.Text = txt.ToUpper
                    box.Font = New Font("Comic Sans MS", 30, FontStyle.Regular, GraphicsUnit.Pixel)
                    box.TextAlign = HorizontalAlignment.Center
                Else
                    box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                End If
                box.Text = txt
            Else
                box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                box.TextAlign = HorizontalAlignment.Left
            End If
        End Sub
    Poppa
    Along with the sunshine there has to be a little rain sometime.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: What could cause a running application to just close down ?

    Your error doesn’t appear to be in the code you’ve posted. Does your app contain any COM components or references? You’re making debugging more difficult than is necessary with all of this multiple statements in one or more lines…

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: What could cause a running application to just close down ?

    Quote Originally Posted by Poppa Mintin View Post
    I've changed the code in Sub BoxChange to remove the Char but it's made no difference, the app. still just exits as previously described.

    Code:
        Private Sub BoxChange(sender As Object, e As System.EventArgs)
            Dim txt As String, box As TextBox = CType(sender, TextBox)
    
            If box.TextLength > 0 Then
                txt = box.Text.ToUpper : txt = txt.Last
                If txt >= "A" And txt <= "Z" Then
                    box.Text = txt.ToUpper
                    box.Font = New Font("Comic Sans MS", 30, FontStyle.Regular, GraphicsUnit.Pixel)
                    box.TextAlign = HorizontalAlignment.Center
                Else
                    box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                End If
                box.Text = txt
            Else
                box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                box.TextAlign = HorizontalAlignment.Left
            End If
        End Sub
    Poppa
    If txt >= "A" And txt <= "Z" Then
    Use Asc(txt), Asc(“A”) and Asc(“Z”)

    box.Text = txt.ToUpper
    Txt is already uppercase

  8. #8
    Lively Member
    Join Date
    Jun 2017
    Posts
    77

    Re: What could cause a running application to just close down ?

    If you are using Windows, you could check the EventViewer, if you havent done so already.
    It might contain some additional information about the exception.

  9. #9
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: What could cause a running application to just close down ?

    Modify your code to create and write a log, write each subroutine name to the log as it is encountered. When your app crashes you will at least be able to read the log and know which specific routine or function of yours is occurring during the actual crash.

    You can also watch the binary operating using something like ProcessExplorer, monitor memory pools and threads
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: What could cause a running application to just close down ?

    Quote Originally Posted by .paul. View Post
    Use Asc(txt), Asc(“A”) and Asc(“Z”)
    Txt is already uppercase
    Thanks .Paul,
    'If txt >= "A" And txt <= "Z" Then' Works correctly.
    It only checks that it's a letter, not a number, because numbers are permitted but in the smaller font. I need the letter to be in upper case, but it can be (is usually) in lower.
    I'd already spotted txt is already in upper.

    I've made 'box' (TextBox) a global variable, so I only have to identify it once. (Sub BoxEnter)
    Entering Sub BoxChange, 'box' is already selected to the correct TextBox.

    I have spotted a potential problem though... 'box.Text = txt' ...calls the subroutine again! (recursion)
    So, yet another modification.
    Code:
       
        Private Sub BoxChange(sender As Object, e As System.EventArgs)
            Static txt As String
    
            If box.Text = txt Then Exit Sub
            If box.TextLength > 0 Then
                txt = box.Text.ToUpper 
                txt = txt.Last
                If txt >= "A" And txt <= "Z" Then
                    box.Font = New Font("Comic Sans MS", 30, FontStyle.Regular, GraphicsUnit.Pixel)
                    box.TextAlign = HorizontalAlignment.Center
                    box.Text = txt
                Else
                    box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                End If
            Else
                box.Font = New Font("Microsoft Sans Serif", 15, FontStyle.Regular, GraphicsUnit.Pixel)
                box.TextAlign = HorizontalAlignment.Left
            End If
        End Sub
    
        Private Sub BoxEnter(sender As Object, e As System.EventArgs)
            box = CType(sender, TextBox)
            box.Select()
        End Sub
    Sadly this has still not entirely solved the problem. It's improved the situation though because I can't now change an existing letter without exiting the box and then re-entering it to make the change.
    The app. hasn't crashed since I made the changes, but it still doesn't answer the question... What could cause a running application to just close down?
    I've learned that it's a 'Protected memory' problem, but I still don't know how or why.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: What could cause a running application to just close down ?

    If txt >= "A" And txt <= "Z" Then'

    That’s an implicit conversion. It’s not testing greater than or equal (or lees than or equal) on the one character strings. It is testing on the Asc code for those strings. Note they are Strings not Chars

  12. #12

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: What could cause a running application to just close down ?

    Quote Originally Posted by .paul. View Post
    If txt >= "A" And txt <= "Z" Then'

    That’s an implicit conversion. It’s not testing greater than or equal (or less than or equal) on the one character strings. It is testing on the Asc code for those strings. Note they are Strings not Chars
    Thanks .Paul,
    If you look back to my original coding for BoxChange, (Post #2) you'll see that originally I used Char and a Unicode value, I only changed it because I didn't (don't) understand why I got the protected memory problem and tried to cure it. (Post #4)

    I think I understand what went wrong, "box.Text = txt" inside a subroutine Called by a change to the text is always going to crash because even changing the text from (say) "A" to "A" is going to re-call the sub. so it's going to run until it 'crashes' or until a different TextBox is selected. (Which is why it didn't crash every time, because generally I moved the cursor to the next TextBox before it 'crashed'.)

    I'm going to mark this thread as resolved because I think I answered the original question.

    Thanks again .Paul.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] What could cause a running application to just close down ?

    If you want to avoid recursion…

    Code:
    Private ignore As Boolean = false
    
    Private Sub BoxChange ‘ etc
        If ignore Then
            ignore = False
            Return
        End If
    
        ‘ your code
    
        ignore = True
        Box.Text = “whatever”
    
    End Sub

  14. #14

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: [RESOLVED] What could cause a running application to just close down ?

    Hi .Paul,

    I don't follow the logic of that. Surely every time the text is changed the subroutine will be called, and every time it's called 'ignore' will be set to false.

    That's why I used a static variable.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] What could cause a running application to just close down ?

    It’s set to True before setting the text within the TextChanged Event. Therefore when it instantly fires TextChanged, it ignores it, sets ignore back to False so it will fire properly from user initiated TextChanges.
    I don’t recommend your programming style. It’s hard to read and debug. You probably learned it from someone who fixes their own bugs in seconds or minutes, instead of hours or days. Keep your code as simple as possible and it’ll be easy to fix or modify…

  16. #16

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: [RESOLVED] What could cause a running application to just close down ?

    Ah... I hadn't noticed that the 'Private ignore As Boolean = false' is outside the subroutine.

    Pop.
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width