Link to home
Start Free TrialLog in
Avatar of Dany Balian
Dany BalianFlag for Lebanon

asked on

Visual Basic 6 Program Quits without any runtime error

Dear Experts,

i have a weird Problem going on since i last compiled a program of mine..
on many computers... if an error occurs, i no longer get runtime errors.. the executable simply gets killed... and disappears..  (it doesn't remain in memory)

if i repeat the same steps to reproduce the error in the visual basic 6 ide, then i get the runtime error, and i'm able to fix it..

however, sometimes my clients call me and say that the program disappears.. and actually they don't know what to say, because they don't have neither a screenshot of the error, nor the error msg.. so it's hard for me to debug the version..

any hints?

p.s. it's happening often, with many clients.. and on different machines.. so it's not a certain machine or operating system issue...

i've seen it.. on windows xp sp3, windows xp sp2, and windows 2003 server sp1

i appreciate your help,

cheers,
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

To start with the obvious: do you have any 'On error resume next' statements or similar in the code?
VB6 apps with subclassing can present these kinds of mysterious bugs too...got any of that?
Avatar of HooKooDooKu
HooKooDooKu

We need details of your error handling.  If you don't do it correctly, or violate some unwritten rules, odd things like this can happen.

As an example, any "On Error" statement clears the contents of Err.  If you (foolishly) include an error handler in the 'Terminate' event of an object (class or form) then the contents of the global Err object will be destroyed if an instance of that object goes out of scope (automatically invoking its Terminate Event) as an Error gets sent up the calling stack.

Here's an example of code that shows this bug:

Public Sub Class1_Terminate()
    On Error Resume Next
    'Clean up code here
End Sub

Public Sub Main()
  On Error Goto DisplayError
  Call MainSub()
  Exit Sub
DisplayError:
  MsgBox( "Err# " & cstr(Err.number) & " - " & Err.description )
End Sub

Public Sub MainSub()
  Dim C as Class1
  Set C = new Class1
  Err.Raise( 5, "Some Error")
  MsgBox "This line of code never executes"
End Sub

What happens is that Main calls MainSub that creates an instance of a Class1 object.  When the error is raised in MainSub, the error is sent to the error handler in Main... but on the way out of MainSub, the Class1 Terminate event gets invoked.  The On Error Resume Next clause clears the contents of Err.  Once the Terminate event is complete, the error is sent to the Main Error handler "DisplayError", where the following message is displayed in a message box:
    Err# 0 -
because Err.number is 0 and Err.description is blank (even though you explicately raise error # 5 with some made-up description).



I could (and would if VB wasn't such a dying language) write an artical on the proper way to keep control over errors.  In this sort of situation, what I've learned to do is create a bunch of Error handler helper functions.

For example, I've defined in my code a Type by the name TypeError where the elements of the type match the elements of the Err object.  I then have Terminate Events that look like this:

Public Sub Object_Termnate
Dim theError as TypeError
    ERROR_IfActiveThenSave( theError )
    On Error Resume Next
    'Execute clean up code
    ERROR_IfSavedThenLoad( theError )

The function ERROR_IfActiveThenSave() simply looks to see if Err.number <> 0.  If it is, then it copies the contents of Err to theError.

The function ERROR_IfSavedThenLoad() simply looks to see if theError.number <> 0.  If it is, then it copies the contents of theError to Err.

So if there is an error actively being passed up the call stack when this Terminate event is invoked, the contents of Err are copied to theError, the On Error clause clears the content of Err, the Terminate event executes without raising any errors, and the contents of theError are copied back into Err.
1. Are you encrypting it? Some encryption programs will screw it up so it does that. So you need to change the encryption options.

2. What is the specific runtime error you are getting within the IDE, and please post the code snippet here.

Thanks.
Avatar of Dany Balian

ASKER

thanks all for your participation:

nothing has changed in my code! that's the weird part...

in my ide, errors are normal errors... like invalid use of null while reading from the database, or picture not found in res file.. or whatever.. or invalid parameter type sent to a report!

* i have error handling in all my program...

i use sometimes on error resume next, and catch it on the next line!
or on error goto err_handler: then i handle the error accordingly

* no encryption used in my code!


* regarding classes: yes i have classes and i have custom activex controls in my project.. but haven't changed anything in them lately... anyways, to test ur point of view... i have quoted all "on error resume next" statements.. still same thing occurs..

another question: is there anyway to create a catchall error procedure??

* I don't think "on error resume next" could cause this sort of problem..
* If the App terminates without any message - are you sure there isn't an inadvertent "Unload Me" on a form that is inadvertently the "Last Form Standing"?
* Does this behavior occur on several PCs?

* Windows service packs or Patches are famous for causing new error situations to occur!
(specially with 'custom activex controls')
* Can you temporarily replace the functionality of the activex controls with a normal code module?
A couple of hints:
1) Look at windows application log, sometimes a VB6 app may "disappear" caused by application exceptions (e.g. access violations). In these cases you will find related entries.
2) Check if Windows is configured to disable error reporting: Control panel, System, Advanced, Error Reporting. Make sure you have selected Enable error reporting for Programs (and Windows as well)
@Brian:
- the behavior occurs on several pcs - no recent service packs installed (not even in the past 6 months) (updating is disabled)
- any error that occurs in any form forces the program to disappear without any warnings!
- example: i added a new command button to a form (any form):
sub command1_click()
dim i as integer
i=999999
end sub

this should give overflow! it doesnt, it just quits the program...

@rafunk:
- error reporting is not disabled: it's actually enabled for all programs..
- nothing shows in any of the event groups (system, application, ...)

gonna check my custom controls if they contain anything suspicious and will try also to remove them if i can...

ASKER CERTIFIED SOLUTION
Avatar of BrianVSoft
BrianVSoft
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
already tried recompiling on 2 different computers..
i will try ur strategy of gradually adding components until the system overflows correctly << hehe, i like the expression!

will let u know what happens although nothing has changed in the codes of the components since last 10 builds!
i have some updates! and i have solved the problem but in a weird way, if someone can explain so that i solve my problem in a better way i would be more greatful..

here's what i've done:
- first i've removed completely all my custom user controls and replaced them with some similar native components..
and the project run normally (runtime errors produce normal msgboxes with error descriptions and error numbers)
- i created a new activex control project, and put all my usercontrol in it, compiled the ocx, and referenced it in my project.. and reverted all the native components with my custom controls..  and the project still runs as expected (normal  runtime errors)

finally, thinking that something weird is happening, i removed the reference of the ocx from my project, and i put back the custom controls to the project.. and the problem reappeared! (on errors, the exe just disappears with no runtime error)

any hints about what's going on?

That actually isn't weird.. Our companies "elder VB guys" all remember situations like that..
It could also be that a virus has damaged the ocx? Do you have an old copy in an archive you could dig out and compare bytewise? You might find a few hundred bytes are different.
In any case, none of our VB guys think thats unusual - just typical of microsofts platform.
didn't solve the problem the way i wanted, but guided me in the right way to know which component was making the error! i converted it into ocx and merged it in my project, but still unable to use it when i added to my project as a user control