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

    The probelm with HANDLES

    I am constantly running into the following problem. Suppose I have a class with a HANDLE.

    class T
    {
    HANDLE handle;

    T()
    T(const T & t)

    ~T ()
    }

    T::T()
    {
    handle = CreateEvent (NULL, TRUE, TRUE, NULL);
    }

    T::T(const T & t)
    {
    // What do you do here?

    // Copy the handle from t?

    handle = t.handle;

    // Create a new handle?

    handle = CreateEvent (NULL, TRUE, TRUE, NULL);

    // Ideally, you assign the handle from t and increment the OS
    // handle count. I don't see how you do this. Is there a way?

    :uplicateHandle () doesn;t seem to work
    }


    T:~:T()
    {
    CloseHandle (handle);
    }

    This problem is further compounded when using STL and you push the object into a container. You get temporarary copies, etc.

    What can be done? I don't want event object I create to be on the heap.

    Any ideas?

    Thanks,
    Jim

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    First off, the HANDLE class member variable of class T is private. So first implement a member function that returns a copy of the HANDLE.

    Kuphryn

  3. #3
    Join Date
    Feb 2000
    Posts
    26
    Ok fine. Make the handle member public.

    This still does not solve the problem. When either of the objects goes out of scope, then destructor closes the event handle. The handle is closed therefore useless in the remaining object. This is what I am trying to get at.

    I guess I will need allocate a wrapper class and reference count the HANDLE. Sure would be helpful, if the function DuplicateHandle () would bump and internal counter for us. That way the handle would remain valid until both objects went out of scope.

  4. #4
    Join Date
    Apr 2004
    Posts
    20
    when you write code, use the "code" button on the top of the message editor. that way you won't get smiles on accident or something when you post the message

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    A class that wraps a handle is a perfect example of something that should not be copyable or assignable. So the answer to your
    question is DONT implement operator= or the copy constructor.
    in fact make them private and not implemented to enforce this.


    What you should do is use reference counting like you have mentioned.
    If the objects are used in the context of a single thread then
    the smart pointer at www.boost.org will be perfect for this.


    //edit
    the exact link is
    http://www.boost.org/libs/smart_ptr/shared_ptr.htm
    Last edited by souldog; June 16th, 2004 at 12:57 AM.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

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