Jump to content
  • Advertisement
Sign in to follow this  
pragma Fury

VariantCopy Thread Safety

This topic is 4893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In MSDN, way down at the bottom of the entry for VariantCopy is a little note that simply says
Quote:
Note The VariantCopy method is not threadsafe.
Does anyone have any idea why that note is there? Has anyone got any insight into what VariantCopy is actually doing behind the scenes that isn't safe? To me, for most types of VARIANT, I would assume it's simply doing a memcpy. Obviously it gets more complicated if the type of the VARIANT is VT_UNK or VT_BSTR or VT_ARRAY, etc.

Share this post


Link to post
Share on other sites
Advertisement
VariantCopy() is not thread safe because it performs multiple steps in a non-atomic manner. For example, the destination variant needs to have its resources freed and then the destination gets a copy of the source variant. In a multithreaded environment it is theoretically possible that the destination can get a new value assigned to it between the resource being freed and the new value being assigned. That is a relatively benign example. Worse case scenarios can probably result in heap corruption trashing the value of stored COM objects.

Share this post


Link to post
Share on other sites
This goes for pretty much everything available, and just with arrays, copying an array can be a lengthy process, while VariantCopy is gladly copying, evil Function comes and modifies the part of the array which has already been copied... oh what a horror ;)

So, now you have copied the array, modified it, and yet you have the array you started out with.

This is one of many reasons I presume, but generally, all memory operations are not thread safe unless they have specifically made so.

EDIT: beaten by a better explanation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!