Originally posted by F.Ultra
View Post
How do you know you #include the same runtime when you include the standard library (free) and the POSIX runtimes (_strdup or w/e)?
Does your code even crash at all with malloc? It didn't crash here, neither on Windows VM, nor on Wine, when I used malloc. At this point I'm passing the blame on strdup being from a different runtime than wherever free gets pulled from.
Originally posted by F.Ultra
View Post
When a DLL, or .so, or whatever, calls a callback function, it's in the exact same process and address space. It is literally just a normal call instruction, just as if you did it directly from your code. You have to understand that there is more than one standard library runtime. Let's call them separately (as if with elf versioning) to illustrate the point.
Your app or library links to a given runtime, so let's say it calls malloc@abc but in code it's just "malloc". malloc@abc uses an internal structure like this:
Code:
struct malloc_abc_state { size_t size; char data[]; };
Then you have a library which you'll use its callback and it uses its own malloc/free, let's call them malloc@xyz and free@xyz.
Code:
struct malloc_xyz_state { int extra_data; size_t size; int more_stuff; char data[]; };
But, and here's the thing: all libraries live within the same address space. When the library uses malloc under Windows, it uses malloc@xyz in this example. But all of YOUR calls to malloc are malloc@abc. You also call free@abc at the end.
Since the callback function that you gave it is YOUR code (literally part of your module), it is supposed to call malloc@abc because that's what the dynamic linker will replace it with. What you pass to the library is the address of YOUR code, your code which uses malloc@abc and not malloc@xyz.
So there should be no conflict whatsoever, unless you messed something up with strdup. Like I said did you test with malloc? I get no crash.
Originally posted by F.Ultra
View Post
Originally posted by F.Ultra
View Post
For example, if you make an app with Clang and link a library built with GCC and do this insane thing, it will crash. That's beyond absurd, it's pathetic and stinks of trash code.
Also thanks for giving some examples, those libraries deserve to be in the "hall of shame". Your ne_free is even worse since it's a macro which is substantially different behavior than a function, so code relying on that shameful library won't even be guaranteed the same behavior.
This code:
Code:
int foobar() { int ne_free; unsigned int free = 42; /* ... */ }
Very "solid" library design. I can imagine they put so much thought into it /sarcasm
Shining standard of code there. Of course, Linux libs never cease to amaze me at their poor quality, so whatever.
Comment