0

I want to implement hot code reloading in my program and it seems to work but I'm not sure if it is actually undefined behavior and I am just lucky.

I have a base class shared between a dll and the main program. The dll has a function that returns a pointer to the derived class

Base* getInstance(){return new Derived();}

When I reload the dll I also reload the getInstance function. Calling the virtual function from the base class still works but I think it might be undefined behavior. I'm not sure if I have to reload it. Also, it seems that if I load both instances of the dll and then free the old one instead of freeing the old one and loading the new one it breaks the vtable and crashes on calling the virtual function. How do virtual functions from a dll get loaded? Is my approach ok? What if I want to load at some point 2 instances of the dll.

Aamir
  • 1,974
  • 1
  • 14
  • 18
vlad alex
  • 26
  • 3
  • It's a really bad idea to use a class instance if the dll where it's coming from was unloaded. Why are you doing that? – ssbssa Sep 27 '22 at 10:20
  • I want to be able to Implement hot code reloading. This means recompiling the code at runtime and reloading it while my program is still running. One way is to do it without classes but I want to use classes now so the user can define multiple derived classes to reload during runtime. So I never use the class with the dll is unloaded. I use it with the dll reloaded. – vlad alex Sep 28 '22 at 12:45
  • But that's what I mean, once the dll is unloaded, the class instance can't be used anymore. So when you reload the dll, you need to create a new instance. – ssbssa Sep 28 '22 at 12:58
  • Ok so now I am basically relying on undefined behavior, right? – vlad alex Sep 29 '22 at 12:46
  • That's not what I would call that. Why are you so bent on continuing to use a class instance of an unloaded dll? – ssbssa Sep 29 '22 at 13:37
  • I want the user to be able to add different pieces of code that will be loaded at runtime and then be able to be reloaded while running. (I also have a release version that compiles statically so no dll reloading stuff there) – vlad alex Oct 02 '22 at 08:45
  • That doesn't explain why you want to continue to use the first class instance. You need to create a new class instance after the reload. – ssbssa Oct 02 '22 at 10:34
  • hm yes fair enough – vlad alex Oct 14 '22 at 14:51

0 Answers0