Thread: Called TlsFree but it's still there!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Called TlsFree but it's still there!

    I called TlsFree after storing something in the Thread Local Storage and when I call TlsGetValue(...) it returns the object! Why is this?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by 6tr6tr View Post
    I called TlsFree after storing something in the Thread Local Storage and when I call TlsGetValue(...) it returns the object! Why is this?
    Deallocating a resource doesn't necessarily destroy the "bits" of that resource. So it still might appear to be there, even though it isn't. Accessing it is still wrong.

    As an analogy, imagine that you've been banned from campus. That doesn't mean you can't go on campus, it just means you'll get in trouble if you get caught

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The real question of the day is why are you trying to use a freed buffer? Or were you just curious.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by master5001 View Post
    The real question of the day is why are you trying to use a freed buffer? Or were you just curious.
    Mostly just curious but I also want people using the code to not accidently be able to call get again and have it work during their tests and then have it lead to issues later. How would I be sure to free/delete that data?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by 6tr6tr View Post
    Mostly just curious but I also want people using the code to not accidently be able to call get again and have it work during their tests and then have it lead to issues later. How would I be sure to free/delete that data?
    It *is* freed. The values of the bits are still there, however. If you want, you could clear the buffer to zero (or some other value indicating "been freed"), BEFORE freeing it.

    Accessing freed storage is a common error, which often goes undetected for exactly this reason. Some runtimes will mark storage with special byte values before freeing it, when operating in debug mode, to try to catch these errors.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As explained above, the free function doesn't actually remove the data in the entry, it just says "this index can be used again".

    Something like this seems like a good idea:
    Code:
    void myTlsFree(long &index)
    {
        TlsSetValue(index, 0);
        TlsFree(index);
        index = 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by brewbuck View Post
    Accessing freed storage is a common error
    really? how common? Never seen it happen except on forums like this where people are trying to learn the language

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KIBO View Post
    really? how common? Never seen it happen except on forums like this where people are trying to learn the language
    I've found bugs like that in commercial code. Perhaps not frequently, but it certainly happens.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There were many programs that broke on the transition to Win95 because it was far less forgiving about accessing freed memory than Win 3.x.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2009, 12:05 AM
  2. Replies: 4
    Last Post: 09-21-2008, 02:27 PM
  3. callback from exe called with system()
    By leonv in forum C Programming
    Replies: 3
    Last Post: 01-25-2008, 04:12 PM
  4. AAARG!!! mental block, what is this character called: ')'
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-29-2002, 10:29 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM