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

    passing a structure pointer cross threads with global vector

    I have a vector of structure pointers.
    The vector is in global space so I can access it from worker threads.
    I have a couple of access functions like Pop, Push, and GetSize that do locking on any access to the vector.

    struct mystructure
    {
    _bstr_t value1;
    _bstr_t value2;
    int active;
    mystructure(void){printf("\nConstructer Called!\n");}
    ~mystructure(void){printf("\nDestructer Called!\n");}
    };

    In Thread One:
    I create a structure pointer;
    mystructure* pmystructure = new mystructure;
    Constructor is called!!

    load data items;
    pmystructure->value1="hello";
    etc...

    Push(pmystructure)
    Places it in the vector.

    In Thread Two:
    mystructure * pstruct=NULL;
    return a *mystructure type with Pop().
    pstruct=Pop();

    Then I use the values in that structure.
    cout<<(LPCSTR)pstruct->value1<<endl;

    My issue is when I try to delete the pstruct.
    When i have the following;

    delete pstruct;
    pstruct=NULL;

    The destructor is never called and my heap seems to keep all the memory allocated.


    How do I call the destructor to the structure and there by the smart pointers will call their destructors and free the heap allocation for the struct and its members and allow it back to the OS?


    Hope I am clear on my intent here.

    All help is respected.

    Thank you,
    MGP

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: passing a structure pointer cross threads with global vector

    Can you post a simple console application that we can compile and reproduce the issue?

    gg

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: passing a structure pointer cross threads with global vector

    It looks like you're pushing/popping "new'd" objects, but never deleting all the objects in the global container.

    Viggy

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