By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

System.OutOfMemoryException when showing form

P: n/a
Baz
Hi.

I'm new to this VB.Net mullarkey, and I must say it is proving to be a very
trying experience. Here is the latest in a long line of problems:

The Scenario
=========

I am building an MDI application. The first thing it does is to pop up a
little logon form which gathers and authenticates an SQL Server
username/password. It then whacks these into a couple of global variables
so they are available for any subsequent database access. Here are the
declarations of those variables (they are simply in a regular module):

Friend gstrUserName As String
Friend gstrPassword As String

Now, I have another form which is launched from a menu on the mdi parent.
Here is the code which does that:

Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frmCountry As New frmCountry

frmCountry.MdiParent = Me
frmCountry.Show()

End Sub

BUT, one of the first things frmCountry does is to grab itself a middle-tier
object (a class) so as to populate the lists in some combo boxes, and in
order to do this it needs to pass in the SQL Server credentials mentioned
above i.e. it needs to refer to the two global variables. Hence, I have a
Form Load event procedure as follows (where cfrMain is a user control I have
created which has the combo boxes on it):

Private Sub frmCountry_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objCountries As New midCountry

With objCountries
.UserName = gstrUserName
.Password = gstrPassword
End With
cfrMain.DataSource = objCountries

End Sub

Please feel free to criticise/comment on the general approach I have adopted
here, or any specifics in these code snippets (I am, after all, trying to
learn). However, the particular problem I have is that the statement
frmCountry.Show() throws a System.OutOfMemoryException error. HOWEVER, and
here's the weird bit, if I comment out the following two statements in
frmCountry_Load, then frmCountry shows OK:

.UserName = gstrUserName
.Password = gstrPassword

frmCountry also shows OK if I replace the references to the global variables
with literals e.g.

.UserName = "myuser"
.Password = "mypassword"

It also shows OK if it's not a child form.

So what's going on here? Why does something as apparently trivial as
referencing a couple of global variables result in an out-of-memory error?
And what can I do about it? And should I give up on the mdi application (a
trawl through Google groups shows a LOT of out-of-memory problems when
showing a child form, but no answers that I can find)?

Thanks for listening!

Baz


Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Baz

The first thing I noticed was that you define a variable with the same name
as a class. Whilst not forbidden, I would personally avoid this as it can
cause no end of problems when referencing the instance.

I would be inclined to change your click handler to something like

<code>
Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frm As New frmCountry

frm.MdiParent = Me
frm.Show()

End Sub
</code>

then there is no confusion regarding which object is being referenced. This
may even resolve your problem.

HTH

Charles
"Baz" <bc**@clara.co.uk> wrote in message
news:10****************@lotis.uk.clara.net...
Hi.

I'm new to this VB.Net mullarkey, and I must say it is proving to be a very trying experience. Here is the latest in a long line of problems:

The Scenario
=========

I am building an MDI application. The first thing it does is to pop up a
little logon form which gathers and authenticates an SQL Server
username/password. It then whacks these into a couple of global variables
so they are available for any subsequent database access. Here are the
declarations of those variables (they are simply in a regular module):

Friend gstrUserName As String
Friend gstrPassword As String

Now, I have another form which is launched from a menu on the mdi parent.
Here is the code which does that:

Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frmCountry As New frmCountry

frmCountry.MdiParent = Me
frmCountry.Show()

End Sub

BUT, one of the first things frmCountry does is to grab itself a middle-tier object (a class) so as to populate the lists in some combo boxes, and in
order to do this it needs to pass in the SQL Server credentials mentioned
above i.e. it needs to refer to the two global variables. Hence, I have a
Form Load event procedure as follows (where cfrMain is a user control I have created which has the combo boxes on it):

Private Sub frmCountry_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objCountries As New midCountry

With objCountries
.UserName = gstrUserName
.Password = gstrPassword
End With
cfrMain.DataSource = objCountries

End Sub

Please feel free to criticise/comment on the general approach I have adopted here, or any specifics in these code snippets (I am, after all, trying to
learn). However, the particular problem I have is that the statement
frmCountry.Show() throws a System.OutOfMemoryException error. HOWEVER, and here's the weird bit, if I comment out the following two statements in
frmCountry_Load, then frmCountry shows OK:

.UserName = gstrUserName
.Password = gstrPassword

frmCountry also shows OK if I replace the references to the global variables with literals e.g.

.UserName = "myuser"
.Password = "mypassword"

It also shows OK if it's not a child form.

So what's going on here? Why does something as apparently trivial as
referencing a couple of global variables result in an out-of-memory error?
And what can I do about it? And should I give up on the mdi application (a trawl through Google groups shows a LOT of out-of-memory problems when
showing a child form, but no answers that I can find)?

Thanks for listening!

Baz



Nov 20 '05 #2

P: n/a
Hi Baz

The first thing I noticed was that you define a variable with the same name
as a class. Whilst not forbidden, I would personally avoid this as it can
cause no end of problems when referencing the instance.

I would be inclined to change your click handler to something like

<code>
Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frm As New frmCountry

frm.MdiParent = Me
frm.Show()

End Sub
</code>

then there is no confusion regarding which object is being referenced. This
may even resolve your problem.

HTH

Charles
"Baz" <bc**@clara.co.uk> wrote in message
news:10****************@lotis.uk.clara.net...
Hi.

I'm new to this VB.Net mullarkey, and I must say it is proving to be a very trying experience. Here is the latest in a long line of problems:

The Scenario
=========

I am building an MDI application. The first thing it does is to pop up a
little logon form which gathers and authenticates an SQL Server
username/password. It then whacks these into a couple of global variables
so they are available for any subsequent database access. Here are the
declarations of those variables (they are simply in a regular module):

Friend gstrUserName As String
Friend gstrPassword As String

Now, I have another form which is launched from a menu on the mdi parent.
Here is the code which does that:

Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frmCountry As New frmCountry

frmCountry.MdiParent = Me
frmCountry.Show()

End Sub

BUT, one of the first things frmCountry does is to grab itself a middle-tier object (a class) so as to populate the lists in some combo boxes, and in
order to do this it needs to pass in the SQL Server credentials mentioned
above i.e. it needs to refer to the two global variables. Hence, I have a
Form Load event procedure as follows (where cfrMain is a user control I have created which has the combo boxes on it):

Private Sub frmCountry_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objCountries As New midCountry

With objCountries
.UserName = gstrUserName
.Password = gstrPassword
End With
cfrMain.DataSource = objCountries

End Sub

Please feel free to criticise/comment on the general approach I have adopted here, or any specifics in these code snippets (I am, after all, trying to
learn). However, the particular problem I have is that the statement
frmCountry.Show() throws a System.OutOfMemoryException error. HOWEVER, and here's the weird bit, if I comment out the following two statements in
frmCountry_Load, then frmCountry shows OK:

.UserName = gstrUserName
.Password = gstrPassword

frmCountry also shows OK if I replace the references to the global variables with literals e.g.

.UserName = "myuser"
.Password = "mypassword"

It also shows OK if it's not a child form.

So what's going on here? Why does something as apparently trivial as
referencing a couple of global variables result in an out-of-memory error?
And what can I do about it? And should I give up on the mdi application (a trawl through Google groups shows a LOT of out-of-memory problems when
showing a child form, but no answers that I can find)?

Thanks for listening!

Baz



Nov 20 '05 #3

P: n/a
Baz
Thx for the reply Charles. Good point about the naming.

I have changed the design so that UserName and Password, instead of being
properties of the middle tier object, are instead arguments of it's methods.
This involved moving the code that references the global variables, so that
it is now done by the user control, cfrMain, rather than in the Load event
of the form. It still gets done when the form loads, but it's a couple of
calls further down the stack, as it were. And...the problem went away.

Seems to be (yet another) bug in .Net. A few days ago I wasted a couple of
hours discovering the unreliability of setting the SelectedIndex for a bound
combo box to -1. Is programming in VB.Net always this frustrating?

Baz

"Charles Law" <bl***@nowhere.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
Hi Baz

The first thing I noticed was that you define a variable with the same name as a class. Whilst not forbidden, I would personally avoid this as it can
cause no end of problems when referencing the instance.

I would be inclined to change your click handler to something like

<code>
Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frm As New frmCountry

frm.MdiParent = Me
frm.Show()

End Sub
</code>

then there is no confusion regarding which object is being referenced. This may even resolve your problem.

HTH

Charles
"Baz" <bc**@clara.co.uk> wrote in message
news:10****************@lotis.uk.clara.net...
Hi.

I'm new to this VB.Net mullarkey, and I must say it is proving to be a

very
trying experience. Here is the latest in a long line of problems:

The Scenario
=========

I am building an MDI application. The first thing it does is to pop up a little logon form which gathers and authenticates an SQL Server
username/password. It then whacks these into a couple of global variables so they are available for any subsequent database access. Here are the
declarations of those variables (they are simply in a regular module):

Friend gstrUserName As String
Friend gstrPassword As String

Now, I have another form which is launched from a menu on the mdi parent. Here is the code which does that:

Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frmCountry As New frmCountry

frmCountry.MdiParent = Me
frmCountry.Show()

End Sub

BUT, one of the first things frmCountry does is to grab itself a

middle-tier
object (a class) so as to populate the lists in some combo boxes, and in
order to do this it needs to pass in the SQL Server credentials mentioned above i.e. it needs to refer to the two global variables. Hence, I have a Form Load event procedure as follows (where cfrMain is a user control I

have
created which has the combo boxes on it):

Private Sub frmCountry_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objCountries As New midCountry

With objCountries
.UserName = gstrUserName
.Password = gstrPassword
End With
cfrMain.DataSource = objCountries

End Sub

Please feel free to criticise/comment on the general approach I have

adopted
here, or any specifics in these code snippets (I am, after all, trying to learn). However, the particular problem I have is that the statement
frmCountry.Show() throws a System.OutOfMemoryException error. HOWEVER,

and
here's the weird bit, if I comment out the following two statements in
frmCountry_Load, then frmCountry shows OK:

.UserName = gstrUserName
.Password = gstrPassword

frmCountry also shows OK if I replace the references to the global

variables
with literals e.g.

.UserName = "myuser"
.Password = "mypassword"

It also shows OK if it's not a child form.

So what's going on here? Why does something as apparently trivial as
referencing a couple of global variables result in an out-of-memory error? And what can I do about it? And should I give up on the mdi application

(a
trawl through Google groups shows a LOT of out-of-memory problems when
showing a child form, but no answers that I can find)?

Thanks for listening!

Baz




Nov 20 '05 #4

P: n/a
Baz
Thx for the reply Charles. Good point about the naming.

I have changed the design so that UserName and Password, instead of being
properties of the middle tier object, are instead arguments of it's methods.
This involved moving the code that references the global variables, so that
it is now done by the user control, cfrMain, rather than in the Load event
of the form. It still gets done when the form loads, but it's a couple of
calls further down the stack, as it were. And...the problem went away.

Seems to be (yet another) bug in .Net. A few days ago I wasted a couple of
hours discovering the unreliability of setting the SelectedIndex for a bound
combo box to -1. Is programming in VB.Net always this frustrating?

Baz

"Charles Law" <bl***@nowhere.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
Hi Baz

The first thing I noticed was that you define a variable with the same name as a class. Whilst not forbidden, I would personally avoid this as it can
cause no end of problems when referencing the instance.

I would be inclined to change your click handler to something like

<code>
Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frm As New frmCountry

frm.MdiParent = Me
frm.Show()

End Sub
</code>

then there is no confusion regarding which object is being referenced. This may even resolve your problem.

HTH

Charles
"Baz" <bc**@clara.co.uk> wrote in message
news:10****************@lotis.uk.clara.net...
Hi.

I'm new to this VB.Net mullarkey, and I must say it is proving to be a

very
trying experience. Here is the latest in a long line of problems:

The Scenario
=========

I am building an MDI application. The first thing it does is to pop up a little logon form which gathers and authenticates an SQL Server
username/password. It then whacks these into a couple of global variables so they are available for any subsequent database access. Here are the
declarations of those variables (they are simply in a regular module):

Friend gstrUserName As String
Friend gstrPassword As String

Now, I have another form which is launched from a menu on the mdi parent. Here is the code which does that:

Private Sub mnuCountries_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuCountries.Click

Dim frmCountry As New frmCountry

frmCountry.MdiParent = Me
frmCountry.Show()

End Sub

BUT, one of the first things frmCountry does is to grab itself a

middle-tier
object (a class) so as to populate the lists in some combo boxes, and in
order to do this it needs to pass in the SQL Server credentials mentioned above i.e. it needs to refer to the two global variables. Hence, I have a Form Load event procedure as follows (where cfrMain is a user control I

have
created which has the combo boxes on it):

Private Sub frmCountry_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objCountries As New midCountry

With objCountries
.UserName = gstrUserName
.Password = gstrPassword
End With
cfrMain.DataSource = objCountries

End Sub

Please feel free to criticise/comment on the general approach I have

adopted
here, or any specifics in these code snippets (I am, after all, trying to learn). However, the particular problem I have is that the statement
frmCountry.Show() throws a System.OutOfMemoryException error. HOWEVER,

and
here's the weird bit, if I comment out the following two statements in
frmCountry_Load, then frmCountry shows OK:

.UserName = gstrUserName
.Password = gstrPassword

frmCountry also shows OK if I replace the references to the global

variables
with literals e.g.

.UserName = "myuser"
.Password = "mypassword"

It also shows OK if it's not a child form.

So what's going on here? Why does something as apparently trivial as
referencing a couple of global variables result in an out-of-memory error? And what can I do about it? And should I give up on the mdi application

(a
trawl through Google groups shows a LOT of out-of-memory problems when
showing a child form, but no answers that I can find)?

Thanks for listening!

Baz




Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.