Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:36:19+00:00 2026-05-28T20:36:19+00:00

I implement a new module using shared_ptr etc. in our legacy app, however I

  • 0

I implement a new module using shared_ptr etc. in our legacy app, however I get a access violation, when shared_ptr is calling the destructor.

app:

case ENUM_DATA:
{
    std::tr1::shared_ptr<CDataMsg> msg(new CDataMsg(_stringmsg)); // _stringmsg is initialized before
    Process(msg);
    break;
}

Process():

bool Process(std::tr1::shared_ptr<CDataMsg> msg)
{
    try
    {
        switch (msg->getDataType())
        {
            case ENUM_MYDATATYPE:
            {
                std::tr1::shared_ptr<CMyData> base(msg->getData());
                std::tr1::shared_ptr<CMyDataChild> data(std::tr1::static_pointer_cast<CMyDataChild>(base));

                // do some stuff with data
                std::tr1::shared_ptr<CRequest> request(new CRequest(data->getParam1(), data->getParam2()));
                handler->AddRequest(request->getBin());
                break;
            }
            default:;
        }
        return true;
    }
    catch (...)
    {
        // exception handling
    }
    return false;
}

Destructor:

CDataMsg::~CDataMsg()
{
    if (m_data)
        delete m_data;
    m_data = NULL;
}

m_data is a CMyData* (cannot be changed at this point).

CDataMsg is a container, which holds data of type CMyData. CmyDataChild is a subclass of CMyData, which is used here.

I have breakpoint in the destructor, but the debugger stops only, when shared_ptr is calling it and then I get the access violation already.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:36:20+00:00Added an answer on May 28, 2026 at 8:36 pm

    As you have confirmed in your comment msg->getData() returns a pointer to a member variable of msg (presumably m_data) and it will be deleted when this switch block scope exits:

    case ENUM_MYDATATYPE:
    {
        std::tr1::shared_ptr<CMyData> base(msg->getData());
        std::tr1::shared_ptr<CMyDataChild>
            data(std::tr1::static_pointer_cast<CMyDataChild>(base));
    
        // do some stuff with data
        std::tr1::shared_ptr<CRequest>
            request(new CRequest(data->getParam1(), data->getParam2()));
        handler->AddRequest(request->getBin());
        break;
    }
    

    The destructor of msg will be invoked later when this switch block scope exits:

    case ENUM_DATA:
    {
        std::tr1::shared_ptr<CDataMsg> msg(new CDataMsg(_stringmsg));
        Process(msg);
        break;
    }
    

    and attempt to redelete the member variable m_data.

    Also:

    case ENUM_MYDATATYPE:
    {
        std::tr1::shared_ptr<CMyData> base(msg->getData());
        std::tr1::shared_ptr<CMyDataChild>
            data(std::tr1::static_pointer_cast<CMyDataChild>(base));
        ...
    }
    

    data is pointing to the same object as base. When this scope exits base will be deleteded twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Flex SDK and trying to implement a simple project using Doug
For the new computer science assignment we are to implement a list/array using an
I am using the Javascript Module Pattern to try and implement C# enumeration-like functionality.
I am trying to implement the Strategy design pattern using interfaces. However, while developing
My organization is getting ready to implement a new system, which is a asp.net
I want to implement user stories in a new project where can i find
I am new to C programming and I'd like to implement chmod command on
I am new to Objective C and I am trying to implement an async
I'm new to python but I've run into a hitch when trying to implement
I'm quite new to C and I'm trying to implement a binary tree in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.