Solved

Unloading the form visual basic is crashing

Posted on 2006-06-15
28
418 Views
Last Modified: 2013-11-25
Hi

My application has been running fine, I write a lot of info to a file when the application closes. VB has started crashing every time I unload the program any ideas why? I use set frm = nothing for each form.
0
Comment
Question by:eneate
  • 11
  • 8
  • 6
  • +2
28 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 50 total points
ID: 16909881
Can you post the complete error here.

Try an exception handling with your form unload
Something like this
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo formUnloadError
  Set frm = Nothing
  Exit Sub
formUnloadError:
  MsgBox Err.Number & " " & Err.Description
End Sub
0
 
LVL 17

Expert Comment

by:inthedark
ID: 16909998
Are you doing the processing in the form_load event?

If the form_load event closes this will cause the code that launched it to fail. So work around this:

Private Sub Form_Activate()

Static bDone As Boolean
' this will allow the code to activate only once.
If Not bDone Then
    bDone = True
    RunProcess ' call the processing that needs to be done
    Unload Me
End If

End Sub

But as Dhaest  suggests we need to know what you application is doing with the form.  You can have an application without a form, or only use a form to display progress. You can place all you code in Sub Main() within a module.

0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16910092

have you been subclassing on the form?


if you did, make sure this is terminated properly
0
 

Author Comment

by:eneate
ID: 16910476
I have commented out all the unload command that write info to the external txt file, and added the error code, I don't get any errors just vb has encountered a problem and will close.
0
 
LVL 17

Expert Comment

by:inthedark
ID: 16910509
What SP of VB are you using? Also have you tried re-booting (sometimes works you know Windows)?
0
 
LVL 9

Expert Comment

by:Naveen Swamy
ID: 16910559
if IDE is closing then it might have clashed with windows internal apis while working on your code or you could be using the subclassing or hooking your application, but if it happens to your compiled and built exe then it means there is some flaw in the code.
0
 
LVL 9

Assisted Solution

by:Naveen Swamy
Naveen Swamy earned 50 total points
ID: 16910572
update your windows and also update your ide with latest service pack as suggested by inthedark
0
 

Author Comment

by:eneate
ID: 16910714
I've closed a few applications (as I've done before) reopened a different piece of software which loaded and closed fine. I've then re opened my application and it's working!!
I'm removing the commentg out on each section to see if I can see the problem. It's worrying not knowing why? It has occassionally crashed before but hey it's microsoft!
This was constant, it crashed consistantly every time, and in the built exe, but only there after a while not straight away.
I am using vb 6 pro with service pack 6 on win xp pro with all the latest windows updates. It is running on a laptop but it is a high spec sony so it should be ok I have 1 gig of ramm 533 mhz bus speed 80 gig hd etc.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16911086

that is really weird

do you have a timer running?
0
 

Author Comment

by:eneate
ID: 16911302
Yes I do use a timer but not with any of these functions after the error vb crashes
0
 
LVL 13

Assisted Solution

by:Mark_FreeSoftware
Mark_FreeSoftware earned 100 total points
ID: 16911525

try to disable it and see if the app is still crashing
0
 

Author Comment

by:eneate
ID: 16911767
I have disables the call for the logged on username in windows and disabled the time. The app does seam to work better but when I unloaded it it crashed vb.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16911791

do you have some functions or subs that are running continue?
0
 

Author Comment

by:eneate
ID: 16911796
Guihigh.Text4.text = Environ("USERNAME")
Name = Guihigh.Text4.text

this is the code I use to get the logged on user named and it is definitly causing the error 14
i dim name as string, when I disable this the function works without any problems.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16911811

change the variable name to somthing other pls and try if it works then?
0
 
LVL 17

Expert Comment

by:inthedark
ID: 16911847
See comments in your other question about using a variable called Name:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21886952.html
0
 

Author Comment

by:eneate
ID: 16912143
YEP you are right removing the name as a variable is definitly helping I havn't had that message since, I'll keep running it for a while and see how we go....

Thanks ever so, the help is brill - saves digging out that pile of vb books that are propping the door open!
0
 

Author Comment

by:eneate
ID: 16912340
It's still crashing, vb that is after I unload the app. It doesn't do it all the time just every few loads. IT does it if an additional window is open and if all additional windows are closed.
Here is the full onload code, I ahve tried commenting it out but it still crashes, I thought it was ok and un commented each section and it appeared to work, then it started again.

Sorry these points will be hard earned.

 On Error GoTo form_Unload_Error
 
Dim file As String
Const filename As String = "c:\ipwin5\tools.ini"
Dim x As String
Dim y As String
Dim mytab As String
Dim Slider1 As String
Dim Slider2 As String
Dim Slider3 As String
Dim Value As String

file = "C:\IPWin5\TBInstance.ini"

If FileExists(file) Then

Open file For Input As #1 'get current instance values from ini file
   Input #1, inst1 '1 if instance 1 is in use, else 0
    Input #1, inst2 '1 if instance 2 is in use, else 0
   
Close #1

If instance = 1 Then inst1 = 0 Else inst2 = 0 'free this instance in ini file
'update entries in instance file
Open file For Output As #1 'save available instance values to ini file
    Print #1, inst1
    Print #1, inst2
Close #1

'This codes saves last window position and current tab

x = Trim(Str(Me.Left / 15))
y = Trim(Str(Me.Top / 15))
mytab = Trim(Str(Guihigh.SSTab1.Tab))

'determine which occurrence of toolboxes we are using, first or second

If instance = 1 Then
Call WritePrivateProfileString("tbwinpos1", "x", x, filename)
Call WritePrivateProfileString("tbwinpos1", "y", y, filename)
Call WritePrivateProfileString("tbwinpos1", "mytab", mytab, filename)

Else

Call WritePrivateProfileString("tbwinpos2", "x", x, filename)
Call WritePrivateProfileString("tbwinpos2", "y", y, filename)
Call WritePrivateProfileString("tbwinpos2", "mytab", mytab, filename)

End If

'write the slider values to the tools.ini file
'**********************************************

Call WritePrivateProfileString("sliders", "slider1", Guihigh.Text1.text, filename)
Call WritePrivateProfileString("sliders", "slider2", Guihigh.Text2.text, filename)
Call WritePrivateProfileString("sliders", "slider3", Guihigh.Text3.text, filename)

'write the size of the toolboxes to the tools.ini file
'*****************************************************

If Guihigh.height = 10710 Then
Value = 0
End If

If Guihigh.height <= 1335 Then
Value = 1
End If

If Guihigh.height = 14700 Then
Value = 2
End If

Call WritePrivateProfileString("tools_size_hr", "value", Value, filename)

'ensure that all the form have been unloaded

Dim frm As Form
For Each frm In Forms
     Unload frm
Next
 
Set Guihigh = Nothing
End If
Exit Sub

error1:

  MsgBox err.Number & " " & err.Description
  Exit Sub

form_Unload_Error:
Resume error1
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16912651

try this code:


   Dim i As Integer
   For i = Forms.Count - 1 To 1 Step -1
      Unload Forms(i)
   Next


instead of this code:


Dim frm As Form
For Each frm In Forms
     Unload frm
Next
0
 
LVL 17

Expert Comment

by:inthedark
ID: 16912666
>Sorry these points will be hard earned.
Harder for you cos you are the one with the error!  But don't worry we have all been there before. And even if we don't win the points we win the knowledge.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16912685

oh, and your error handling looks a bit weird:

error1:
  MsgBox err.Number & " " & err.Description
  Exit Sub
form_Unload_Error:
Resume error1


is the same as:

form_Unload_Error:
  MsgBox err.Number & " " & err.Description
  Exit Sub




//
@inthedark:

and we have the fun of trying to find the good solution!
0
 

Author Comment

by:eneate
ID: 16915886
Yo

I am using code to write a history file into a text box in a form. When it executes the error command I get the error then vb crashes. It will run several times then crash completly un predictably, differnt times. It's driving me mad!!
Here is the code see what you think

Dim RetDateTime As Variant
Dim appdate As Variant
Dim apptime As Variant
Dim user As String
Const filename As String = "C:\ipwin5\tools.ini"
Dim newname As String * 100
Dim Buffer As String
Dim DataString As String

frmaudit.Show

appdate = Date
apptime = Now
Dim outputlog As String
outputlog = frmaudit.Text1
 Dim dinfo As IPDOCINFO
Dim DocID As Integer, hVri As Integer
Dim imageName As String * 50
Dim audit_trail As String

On Error GoTo errHand

Environ ("DOMAINNAME")
Guihigh.Text4.text = Environ("USERNAME")
user = Guihigh.Text4.text

'get the date and time from the computer system

RetDateTime = "Current Time : " & apptime & "Todays Date : " & appdate

'check to see if an image is open, if not do nothing
'*****************************************************

ret = IpDocGet(GETNUMDOC, 0, DocID)
ret = IpDocGet(GETDOCINFO, DOCSEL_ACTIVE, dinfo)

If DocID < 1 Then
GoTo end_separate

Else

'If an image is open proceed to get the information for the audit trail
'***********************************************************************

'Get the filename of the open image

'If no text has been written to the text file, this is the first process tobe run on the image, read the name of the image
'from the top image string.
'If a line of text is already present, the filename will have been written to the tools.ini file, the name should now be retrieved
'from here and not the image. The image name string will continue to change but the audit trail will still cntain the same image information
'********************************************************************************************************************************************

'If no text is in the audit trail box, write the filename to the ini file
'************************************************************************

If frmaudit.Text1.text = "" Then

newname = get_image_name
frmaudit.Text2.text = newname


'write the image name to the ini file

Call WritePrivateProfileString("Audit", "Filename", newname, filename)

'write the audit trail information to the open text box, get the name of the process that has been performed from the text box in the main tools window.
'*******************************************************************************************************************************************************

audit_trail = "User:" & "  " & user & "  " & Date & "  " & Time & "  " & Guihigh.Text5.text & "    " & "Image Name:" & "  " & newname
WriteLineToTextbox frmaudit.Text1, audit_trail

'**********************************************************************************************************************************
'If the audit trail box is not empty read the filename from the tools.ini file and write the information to the audit trail window
'**********************************************************************************************************************************

ElseIf frmaudit.Text1.text <> "" Then

'read the name of the image from the ini file and pass it to the audit trail

'Buffer = String$(260, Chr$(13))
'Call GetPrivateProfileString("Audit", "Filename", vbNullString, Buffer, Len(Buffer), filename)
'DataString = StripChr(Buffer)
' = Buffer 'buffer contains the text from the ini file
 newname = frmaudit.Text2.text

audit_trail = "User:" & "  " & user & "  " & Date & "  " & Time & "  " & Guihigh.Text5.text & "    " & "Image Name:" & "  " & newname
             
'write the audit trail information to the open text box, get the name of the process that has been performed from the text box in the main tools window.
'*******************************************************************************************************************************************************

          WriteLineToTextbox frmaudit.Text1, audit_trail
         
  End If
end_separate:
End If
Exit Function

errHand:
MsgBox err.Number & "  " & err.Description
Exit Function
end function
0
 
LVL 17

Accepted Solution

by:
inthedark earned 300 total points
ID: 16918297
You need this IDE function. So before you set an on error do this.

If Not IDE Then
    On Error GoTo ......
End If

You need to do this before all on error statements.

Public Function IDE(Optional Genuine As Boolean) As Boolean

' Returns True if running in debug (IDE) mode
'         False if running in an EXE

'Notes:

' Set LiveMode = True in the declarations to override so test app as EXE

'Example:

' If ADO.IDE Then
'   Stop
' End If

' Also when you realy want to know if in IDE before using function that
' cause IDE to hang
' If Not IDE(True) Then
'   Application.Lock ' Will cuase IDE to hang and need to reboot machine
'   Application("HitCount") = Application("HitCount") + 1
'   Application.UnLock
'Else
'   Application("HitCount") = Application("HitCount") + 1
' End If

If Not Genuine And LiveMode Then
    IDE = False
    Exit Function
End If

Static mIDEDone As Boolean
Static mIDE As Boolean

If Not mIDEDone Then ' See below
    ' just do this first time round then store the result
    On Error Resume Next
    Err.Clear
    Debug.Print 1 / 0; ' THis will cuase error in IDE but is ignored in EXE
    If Err.Number <> 0 Then
        mIDE = True
        Err.Clear
    Else
        mIDE = False
    End If
    mIDEDone = True
End If
IDE = mIDE
End Function
0
 
LVL 17

Expert Comment

by:inthedark
ID: 16918307
So I suspect that the on error statement is masking where your error really is so by removing it when in IDE you stand  a change of finding the site of the real error.
0
 

Author Comment

by:eneate
ID: 16918875
Hi

Here we go, to start with it ran ok, then I made it crash again, I am now getting this error message, the instruction at ref memory 0x7c911eo  ref memory at ox2e6e6fd. The memory could not be read click on ok to terminate the program. It seam tobe related to how I put the form ontop and read the form size.
I use this in all my forms

If I comment his out completly I don't seam to get a problem, immediatly. But after the second click so access the line of code if frmaudit.text1.text <> "" then

I get error 14 - out of string space.
a = SetWindowPos(Me.hWnd, HWND_TOPMOST, x, y, Me.width / 15, Me.height / 15, SWP_SHOWWINDOW) 'resize and position

'Call GetPrivateProfileString("Audit", "size", vbNullString, Buffer, Len(Buffer), filename)
'DataString = StripChr(Buffer)
'Size = Buffer  - this reads the ref number 1 or 2 from a text file to determin what size to open the form. I use it elsewhere with no problems.


If I comment out the section below it crashes much less.
'If Size = 1 Then
'frmaudit.height = 6435
'frmaudit.Command5.Caption = text(2177) 'hide
'ElseIf Size = 2 Then
'frmaudit.height = 1135
'frmaudit.Command5.Caption = text(2178) 'show
'End If

Thoughts?
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16918972
>>= text(2177)

i assume this is a function you ahve written yourself?


try not to use names that are reserved keywords
0
 

Author Comment

by:eneate
ID: 16919000
Hi

Yes, I have written most of these functions, the text call is to load a line of text from an external text file which is placed into an array at load up. It has worked fine until I added the last form.
0
 

Author Comment

by:eneate
ID: 16940954
Thanks to everyone for their input, I have managed to remove most of the problems and osolate their causes. I think the main problem now is how I load the text data from a text file - so plenty more posts on the horizon!!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now