-
Notifications
You must be signed in to change notification settings - Fork 888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic override question #981
Comments
Hi @Bvsemmer -- you are not missing anything; it is tricky to override malloc/free dynamically on Windows since each DLL has its own "namespace". However, almost all DLL's link eventually with the universal C runtime ( (however, in the long run, the goal is still to make overriding malloc/free per-process in Windows easier without the need for the |
Hi @daanx, thank you for your reply! Then I must be doing something wrong. I have made a very simple testcase where the redirect is not working, nonetheless. My main app is linking with MiMalloc and is using the I have also used minject.exe to make sure, the mimalloc dlls are loaded first. This is the output of When I launch with MI_VERBOSE=1, I also see that mimalloc is initialized and that malloc is overwritten: The mimalloc dlls are also loaded: This is my test case: class test
{
public:
__declspec(dllexport) std::string GetString();
}; std::string test::GetString()
{
char* test = new char[128];
memset(test, 0, 128);
const char* t = "test";
memcpy(test, t, 4);
std::string r = test;
delete[] test;
return r;
} My main app, links with this library and calls GetString in it's main function. int main(int argc, char *argv[])
{
{
test t;
std::string s = t.GetString();
}
} When 's' goes out of scope, I crash since std::string is trying to deallocate memory that was not allocated by MiMalloc (in the DLL). I also confirmed this, the allocations in the When I put the exact same code in the main body of my app, the allocations happen correctly, as expected, and no crash is happening: int main(int argc, char *argv[])
{
{
char* test = new char[128];
memset(test, 0, 128);
const char* t = "test";
memcpy(test, t, 4);
std::string r = test;
delete[] test;
}
} Clearly I am overlooking something. |
Hmm, it looks like that should work; I'll try to repro and see what is going on. |
I tried it and for me it just works -- I have checked in your test in the I guess there is some setup issue -- I see you use |
Hey Daan, I have a suspicion, but I am not sure how to fix it. I am using MiMalloc through vcpkg (version When I launch MiMalloc using the default installation, with dynamic override, I get a popup that mimalloc-override.dll is missing. (which seems to be a dependency for mimalloc-redirect.dll) It noticed the vcpkg port file only builds So, I figured to build it myself and copy and manage the dll myself. Probably there is a mismatch, even though MiMalloc seems to launch just fine now. Is the vcpkg version of MiMalloc officially maintained? Here I noticed the last update was 2 months ago, so seemed recent enough for me. Thank you for the swift replies! |
Hi!
I am in the process of doing some experimentation with MiMalloc on Windows and I have a question regarding the dynamic override that MiMalloc provides and the advice to globally override new and delete (with the supplied mimalloc-new-delete.h header).
I always assumed a global override new/delete is bad practice on Windows, since memory allocated in 3rd party dlls, that do not provide a proper memory override hook, will give problems if you free that memory in your own binary. Should the dynamic override somehow prevent this from happening?
I am asking, since I have a very simple app that is using hwinfo as a dependency through vcpkg. vcpkg compiles this library as a dll. This particular library is not really well behaving, since it returns internal memory for the caller to free, but this is besides the point.
I just see that this of course crashes, since hwinfo is not using MiMalloc. Since the allocation happens in the dll, outside of MiMalloc, and the free happens by my library, using MiMalloc.
So, to me this feels using a global override is only possible if you have 100% control over your source code. What am I missing?
The text was updated successfully, but these errors were encountered: