• Pages

  • Tags

  • Recent Posts

  • Recent Comments

  • Archives

  • Fri 25 Jan 2008

    Visual Studio 6 vs 2005 allocators

    Published at 17:43   Category C++  

    (If you didn’t sleep enough, don’t try to understand the following)

    If one day you’re in Visual Studio 2005, creating an object that comes from a DLL compiled with Visual Studio 6, you might run into some allocators issues. Normally things go smoothly because your object is allocated and deallocated with the VC2005 allocator if you create it yourself, or by the VC6 allocator if you’re using specific creation functions that come with your DLL.

    What can become very tricky is when this object has a virtual destructor, which means that your object was allocated with the 2005 allocator, but is deallocated with the VC6 allocator. This is not good, and will probably result in a crash nearly impossible to solve unless the above-mentionned DLL is re-built for 2005. Which may not always be possible if you the owner of the DLL doesn’t want to do it himself or give his source code.

    Here’s a solution (I’m really happy we have code gurus here..) : allocate the memory for this object using the VC6 allocator. How do you do that? Easy (aha)! Simply load the VC6 runtime dll (msvcrt), find the pointer to the allocator function, and call it !

    vc6handle = LoadLibrary(TEXT(”msvcrt”));

    typedef void* (*vc6_malloc) (size_t _Size);
    vc6_malloc vc6funcptr = (vc6_malloc) GetProcAddress(vc6handle,”malloc”);
    TheClass* toto = (TheClass*) vc6funcptr(sizeof(*TheClass));
    toto = new (toto) TheClass;

    FreeLibrary(vc6handle);

    You’ll notice that the new function takes the buffer that was allocated as a parameter. This is not the regular C++ allocator, but a custom one that can take a previously allocated buffer.

    I won’t even explained how they did understand the problem …

    Any question? =)

    Tags: , ,


    Related posts

    7 Comments »

    1. “… a custom one that can take a previously allocated buffer” … this custm allocator is called a placement new. I now we are not a advanced C blog but it has to be said :)

      and no, I am not the guru who help cb …..

      Comment by smithbob — Friday, 25 January 2008 @ 20:52

    2. thanks bob =)
      you’re a guru anyway =)

      Comment by cb — Saturday, 26 January 2008 @ 2:27

    3. Allocating objects in a DLL and requesting user code to delete them (or vice versa) is misconception however…

      Comment by YR — Saturday, 26 January 2008 @ 16:06

    4. All this is Windows’ fault.
      ;-)

      Comment by JulienW — Sunday, 27 January 2008 @ 13:59

    5. At least we didn’t have to recompile our kernel to fix that ;)

      Comment by cb — Sunday, 27 January 2008 @ 14:19

    6. “Allocating objects in a DLL and requesting user code to delete them (or vice versa) is misconception however…” …

      yes but some times you do have the choice because you use/integrate a third party component …

      Comment by smithbob — Wednesday, 30 January 2008 @ 17:05

    7. Well, it might be true that sometimes you don’t have a choice if you’re integrating 3rd party components - but to be true, components written like this are VERY poorly designed and you should probably think twice about integrating those into your projects. Everybody ought to know that memory allocation and freeing have to be done using the same runtime. This is not even limited to VC6 vs. 2005, but can happen in different runtime versions for a given compiler, too.

      Comment by Celvin — Monday, 24 March 2008 @ 19:09

    RSS feed for comments on this post. TrackBack URI

    Leave a comment



  • Recent Posts

  • Recent Comments

  • Archives