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 8113861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:58:10+00:00 2026-06-06T02:58:10+00:00

I know this must be a n00b question, but I have to implement a

  • 0

I know this must be a n00b question, but I have to implement a mockup client-server sequential interaction application, and because the number of client-server calls varies, I cannot just iterate the steps in an external function, always fetching data from the client and then forwarding it to the server and vice-versa, so I need to make my Server and Client classes be aware of each other so that they can call their public methods between themselves. One approach would be to design both as Singletons, but I was hoping to do it in a simpler way, more precisely using a circular reference: the client stores a reference to the server and the server stores a reference to the client. I am aware that this might not be a good approach, and it could cause the call stack to explode when it becomes too deep, so any improvements to my design are welcomed.

In order to achieve the described implementation, I thought that I could use std::shared_ptr, because std::unique_ptr won’t work if I also want to prevent the two variables from main from getting clobbered when I call the two setters (right?). So, this is what I have (simplified code):

#include <iostream>
#include <memory>

class Server;

class Client
{
public:
    void SetServer (const Server &server);
private:
    std::shared_ptr<const Server> server;
};

void Client::SetServer (const Server &server)
{
    this->server = std::shared_ptr<const Server>(&server);
}

class Server
{
public:
    void SetClient (const Client &client);
private:
    std::shared_ptr<const Client> client;
};

void Server::SetClient (const Client &client)
{
    this->client = std::shared_ptr<const Client>(&client);
}

int main ()
{
    Server server;
    Client client;

    server.SetClient(client);
    client.SetServer(server);

    //Here I ask the client to start interacting with the server.
    //The process will terminate once the client
    //exhausts all the data it needs to send to the server for processing

    return 0;
}

Unfortunately, my code seems to try to call the Client and Server (implicit) destructors multiple times, or some similar nasty thing, and I’m certain that this is caused by my poor understanding of how std::shared_ptr is supposed to work. Please advise.

  • 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-06-06T02:58:11+00:00Added an answer on June 6, 2026 at 2:58 am

    You allocate your Server and Client instances on the stack, and they will be deleted when main() exits. You don’t want std::shared_ptr deleting them as well. Thus, there are two solutions:

    1. Use unmanaged pointers within Client and Server, and manage their lifetimes externally.

    2. Use managed pointers everywhere, including main(). Do note that shared_ptr implies ownership. In the current design, a Server owns the client, but also a Client owns the server. That’s a circular reference: by default, they’ll never be freed, unless you reset one of those pointers while you still can.

      As an example, you can decide that the clients keep the server alive, thus last vanishing client will bring down the server if there are no other shared_ptrs pointing at it. The server would have a weak_ptr to the client(s), but the clients would have a shared_ptr to the server.

    class Client;
    class Server;
    
    class Client
    {
    public:
        void SetServer (const std::shared_ptr<const Server> &server);
    private:
        std::shared_ptr<const Server> server;
    };
    
    void Client::SetServer (const std::shared_ptr<const Server> &server)
    {
        this->server = server;
    }
    
    class Server
    {
    public:
        void SetClient (const std::weak_ptr<const Client> &client);
    private:
        std::weak_ptr<const Client> client;
    };
    
    void Server::SetClient (const std::weak_ptr<const Client> &client)
    {
        this->client = client;
    }
    
    
    int main()
    {
      std::shared_ptr<Server> server(new Server);
      std::shared_ptr<Client> client(new Client);
    
      server->SetClient(client);
      client->SetServer(server);
    
      // do stuff
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question must have been covered endless of times, but I've searched
I know there must be a really simple answer to this question, but I
I know this must have been answered before here, but I simply can't find
I know this must be a rookie question, but how do i accomplish that?
I know this must be a trivial question, but I've tried many different ways,
I know this must be an age-old, tired question, but I cant seem to
I know this must be a common question, but take a look: Here i
I know this must be a fairly simple question, but I haven't managed to
I know this must be a super beginner question, but how do I create
I know this probably must be a newbie question, but since I don't know

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.