CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2004
    Posts
    561

    Is it legal to do this?

    Allocating memory for an object on one thread and deleting it on another thread? I thought that was ok and that thread share heap space?

    Maybe this is not the source of the problem and I have look deeper.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Is it legal to do this?

    Should be safe enough, provided you synchronize properly.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Is it legal to do this?

    Quote Originally Posted by Lindley
    Should be safe enough, provided you synchronize properly.
    +1

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Is it legal to do this?

    +1 , but , make sure no matter what, allocaiton and deallocation happen through the same module. Read here:
    http://blogs.msdn.com/oldnewthing/ar...15/755966.aspx
    http://social.msdn.microsoft.com/For...-7a0969366b1a/

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Is it legal to do this?

    Quote Originally Posted by Rigel
    Allocating memory for an object on one thread and deleting it on another thread? I thought that was ok and that thread share heap space?

    Maybe this is not the source of the problem and I have look deeper.

    Horribly Illegal, and the software police are on their way to arrest you.

    Seriously, like the others pointed out, it is generally 100% safe if:

    1) You are allocating/releasing the the general heap
    2) You have the proper synchronization in place.

    The reason I am responsding is that there is also the concepts to TLS [Thread Local Storage] and some environments provide access for a dedicated heap along with this. IF you are using TLS, then the memory refereces are only guaranteed to be valid for the thread in question, and any access (not just a new/delete) is illegal.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured