Error - object reference not set to an instance of an object

moster67

Expert
Licensed User
Longtime User
Hello,

When running a sub a second time in an application (in the IDE), I get this error "object reference not set to an instance of an object". It does not happen the first time the sub is being executed. Objects being used are forms and tables.

I have attached a sample program which reproduces the error.

1) open up the Basic4PPC-project
2) run the solution (ensure that the 2 csv-files are in the same directory)
3) click the button "START"
4) click on an item from the loaded form. Now another form should load with other items (filtered from the previous form)
5) exit from the form
6) exit from the form
7) now you should be back at the main-form.
8) click START again and you should get the error mentioned above (an image is attached).

I am stuck! Any ideas? Probably it is something "stupid" which I am not seeing. Maybe a sub??

Thanks.
 

Attachments

  • errormsg.JPG
    errormsg.JPG
    14.3 KB · Views: 207
  • error.zip
    8.5 KB · Views: 198

klaus

Expert
Licensed User
Longtime User
The problem is that you dispose the 2 tables. After having disposed them they don't exist any more !
Attached 2 possibilities:
- testerror1.sbp, not disposing the tables, add an Initialized variable that limits to add the coloumn only during the first run.
- testerror2.sbp, disposing the tables , removing the tables form the forms but declaring them at each start.

Best regards.
 

Attachments

  • testerror1.sbp
    2.2 KB · Views: 182
  • testerror2.sbp
    2.3 KB · Views: 172

moster67

Expert
Licensed User
Longtime User
Once again, many thanks my friend!

With help of your explanations and sample applications, now everything is clear.

The CSV-files (the actual data), in particular "userChannels.csv" can be much larger than those 600 lines furnished with my example. My project is a Remote Control-application and the file is a channel-list and some users have motorized satellite-systems and accordingly their "userChannels.csv" may reach more than 3000-lines. This is the reason why I wanted to dispose the tables so some memory could be saved, also beacuse the channel-list is not always used (not always needed) by the program itself.

Therefore, I prefer your 2nd solution. However, once compiled and trying it on the device, when I close the frmBouquette, the application exits instead of returning to the main-form (with the Start-buton). This does not happen when it's being run in the IDE. Any idea?

Thanks!

The problem is that you dispose the 2 tables. After having disposed them they don't exist any more !
Attached 2 possibilities:
- testerror1.sbp, not disposing the tables, add an Initialized variable that limits to add the coloumn only during the first run.
- testerror2.sbp, disposing the tables , removing the tables form the forms but declaring them at each start.

Best regards.
 

moster67

Expert
Licensed User
Longtime User
Maybe I found the culprit why the sample-application all of sudden exits while being run on the device (and not in the IDE) when closing the frmBouquettes instead of returning to the main-form.

It seems like that the calling of the Dispose-method of the two tables during the close-event of the frmBouquettes makes the application to crash. If I decide not to use the Dispose-method of the tables during the close-event, the application does indeed return to the main-form and does not crash.

I added a button which calls the Dispose-method of the tables on the main-form. At this point, when I click on it, I may indeed dispose the two tables and after a while the available memory has increased (just what I wanted).

So it seems that (at least in my sample application) calling the Dispose-method of a table during the close-event of a form will crash the application. Is this normal?

In order to overcome this, I will somehow try to dispose the tables when I am sure the form "frmBouquettes" has been closed.

An updated sample is attached.
 

Attachments

  • error3.zip
    8.4 KB · Views: 194

klaus

Expert
Licensed User
Longtime User
I have also 'played' with your problem.
Made the same test as you with a button on the main form to dispose the tables which works.

Tried some others but without success. Added a button to the 2 forms hidden by the tables. It works only if you show frmBouquettes and close it. But when you show frmBouquettes, then show frmChannels close frmChannels and close frmBouquettes the program crashes.

To me this looks like a bug.

Attached the test program.

Best regards.
 

Attachments

  • testerror3.sbp
    2.6 KB · Views: 192

moster67

Expert
Licensed User
Longtime User
I noted that in your latest test-version, that the application does not crash when disposing tblChannels during the close-event of the frmChannels but it crashes when disposing tblBouquettes while closing the form frmBouquettes.

So, somehow it seems that the behaviour is not always true.

Maybe someone with more knowledge "behind the machinery" can have a look? Erel? Agraham?
 

klaus

Expert
Licensed User
Longtime User
I noticed the same as you:
The behaviour is strange.
I put the tblChannels.Dispose into the frmChannels.Close routine because this seemed to work.
If you run the program, load frmBouquettes and close it, the program works fine, tblBouquettes.Dispose in frmBouquettes.Close works.
But when loading frmBouquettes and load frmChannels then close frmChannels, there tblChannels.Dispose works. But then when closing frmBouquettes, tblBouquettes.Dispose doesn't work the program crashes !

I think too that Erel should have a look at it.

Best regards.
 

agraham

Expert
Licensed User
Longtime User
I can only make this error when optimised compiled and run on the device. I suspect a timing problem as individually there seems nothing wrong with the program statements.

Erel - this code looks to be in the wrong sequence. The Table is Disposed then one of it's properties is accessed. It's not the problem here (as I corrected and recompiled it) but it doesn't look right and an unfortunately timed thread switch could cause problems.
B4X:
((CEnhancedTable)htControls["__main_tblchannels"]).Dispose();
htControls.Remove(((CEnhancedTable)htControls["__main_tblchannels"]).propName);
 

agraham

Expert
Licensed User
Longtime User
It's something to do with making a selection in a table. If you select a cell in the Channels form it will crash when you close that form. You can stop the crash occurring by clearing the Table first - but I can't at the moment explain why.
B4X:
Sub frmChannels_Close
   tblChannels.Clear
   tblChannels.Dispose
End Sub
 

moster67

Expert
Licensed User
Longtime User
Thanks for your interest Andrew.

I tried clearing the table as suggested but I still get a crash.
See attached project-file.


It's something to do with making a selection in a table. If you select a cell in the Channels form it will crash when you close that form. You can stop the crash occurring by clearing the Table first - but I can't at the moment explain why.
B4X:
Sub frmChannels_Close
   tblChannels.Clear
   tblChannels.Dispose
End Sub
 

Attachments

  • testerror5.sbp
    2.2 KB · Views: 166

klaus

Expert
Licensed User
Longtime User
It works when you add also tblBouquettes.Clear before tblBouquettes.Dispose.

Attached the first programm with the all clear and dispose methods in the frmBouquettes.Close routine.

Best regards.
 

Attachments

  • testerror7.sbp
    1.9 KB · Views: 186

moster67

Expert
Licensed User
Longtime User
Klaus - you're right! It works.

Andrew! Many thanks for taking your time and finding a workaround. :icon_clap:

Now, I am wondering if this is really a bug or "by design" as Microsoft normally puts it.

Once again my thanks my friends!
 

agraham

Expert
Licensed User
Longtime User
I've established that the error is an ObjectDisposedException which doesn't tell us much. I've also managed to get a stack trace from the Exception and the problem is at a very low level in the native drawing code. The DataGrid (Table) seems to have received a LostFocus message after it has been destroyed and then tries to redraw the selected cell. Things then go wrong about 8 calls further down the stack when the native code fails. There is a conditional check on some items before it actually tries the redraw. I suspect that clearing the Table places those items in a state where the conditional fails so it doesn't attempt the redraw and all is fine.

It looks like a bug but I am surprised that I can find nothing about by Googling. You would have thought it might have been seen before. It is also surprising that .NET Framework dumps the application when this occurs. Normally in case of a Basic4ppc exception the error message box remains on screen and the code can usually carry on. It also begs the question as to why the behaviour is different in the IDE!

EDIT : - What's different is that the IDE nulls the DataSource before calling Dispose. When Googling I have found references to unhooking DataSources. If this is really the reason it should go into the optimised compiled code.
 
Last edited:
Top