Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB access violation

Status
Not open for further replies.

vietnam97

Programmer
Sep 18, 2000
48
I am running an app on VB6, SP3. The code runs fine in VB but when I compile it into an EXE, and run it, It would crash. My particular problem happened around when I unloaded the child form, set FormName = Nothing to release it from memory, then showed the calling form, it then gave me an access violation and crashed the application. This is the actual code:

Unload SeparateAccount
Set SeparateAccount = Nothing

where SeparateAccount is the actual form name, and the app crashed when I set it to Nothing.

Thanks,
Bao
 
Hey Bao,

Unless you created a new form using something like

Dim frm as new Form1

you dont have to set it to Nothing at the end. Just unloading it will be fine.

Tuan
 
Hi Tuan,

A couple things I want to mention too:

1) I never declare a variable to store the form reference, I only use the actual form name in my code.

2) I also use some form variables (declared at the module level) so by unloading the form alone, the form variables still maintaining the values, so I had to set it to Nothing to clear them from memory.
 
Hey Bao,

Unless you are dynamically declaring the variables or using them to reference something, you dont need to "clear" them. Once you unload the form, those variables goes out of scope and are deallocated by the system itself. You dont need to do it.

Does removing the Set SeparateAccount = Nothing stop it from crashing?

Tuan
 
Hi vietnam97,

When a form is unloaded using Unload MyForm, VB will release any memory that was used by form-level variables too. The exception is if you declared them using the New operator as venom said. Once this memory is released, the address no longer 'belongs' to your program, it belongs to Windows (or possibly another app already). Trying to set it to Nothing at this point will throw an access violation, just like you're getting. Venom is right, you don't need to set it to nothing unless you've used a cooresponding 'new' operator to create a form object.

Good luck!

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top