UUPharmacometrics / libsoc

A library for creating, writing and reading PharmML standard output files
GNU Lesser General Public License v3.0
8 stars 2 forks source link

Using an SO object between R sessions crashes R studio #5

Open stumoodie opened 6 years ago

stumoodie commented 6 years ago

I was getting the occasional crash related to the SO object and I think I have identified what is causing it. It seems to be that if you use an object that is in your R environment after you restart the R session in R studio then the session crashes. This occurs on the Mac and Windows. To reproduce initialise an So object from a file.

test_so_file <- "UC1_focei_SO.xml"
test_so <- libsoc::so_SO_read(file.path("tests/testthat", test_so_file))

Test is works:

test_so$all_population_estimates()

Restart the R session and try the last command again:

test_so$all_population_estimates()

This crashes the R session. I've attached the R studio screenshot. rcrash

rikardn commented 6 years ago

Very interesting! I don't know exactly what happens when you restart an R session. How can even the variables be retained in the global environment if R was restarted? But, still, I think I know what is causing this. The restart causes the underlying C objects to be freed from memory, but the R object that is somehow retained still points into this memory so we have a case of dangling pointers. There are ways to let R know about this memory and hopefully not free it when restarting.

stumoodie commented 6 years ago

I'll leave it with you. Now I know I'll try and avoid this scenario.

bguiastr commented 6 years ago

Not sure if it could be of any help in this case but you may be interested in this function:

library.dynam(chname = 'libsoc',
              package = 'libsoc',
              lib.loc = .libPaths()[1])