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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:46:34+00:00 2026-05-23T06:46:34+00:00

void ServerManager::addWork(SocketClient *clientSocket, vector<char> data){ Work *work = new Work(clientSocket, data); m_workHandler->addWork(work); } Here

  • 0
void ServerManager::addWork(SocketClient *clientSocket, vector<char> data){
    Work *work = new Work(clientSocket, data);
    m_workHandler->addWork(work);
}

Here I created Work instance and hand the work to the WorkHandler.

m_workHandler->addWork(work);

This code is inserting the work into the queue.

This is one part of my server and if I run this code for long time then the server crashes with error saying like this

what() std::bad_alloc

I looked online and it is an error message that show there isn’t memory space available. So I realized I didn’t free the work object after I am done with using it.

So I was wondering where I should free the work object.

void ServerManager::addWork(SocketClient *clientSocket, vector<char> data){
    Work *work = new Work(clientSocket, data);
    m_workHandler->addWork(work);
    delete work;
}

Can I do this? If I pass the work instance then it increases the reference count?

void WorkHandler::workLoop(){
    printf("WorkHandler::workLoop, called\n");

    while(m_workHandlerRun){
        Work *work = getWork();
        char *pdata = work->getVPointer();

        unsigned char type = pdata[0];

        printf("WorkHandler::workLoop, type %d\n", type);

        Packet *packet = m_packetFactory->createInstance(static_cast<PACKET_TYPES>(type));
        packet->fromByte(pdata);

        delete work;
    }
}

This is the code I handle the work….Should I delete work instance here or both place? Thanks in advance…

  • 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-23T06:46:35+00:00Added an answer on May 23, 2026 at 6:46 am

    Judging by your second bit of code, it looks like the memory leak might actually be from the Packet allocation, because the Work instance is being deleted in what seems like a correct place (in workLoop, not addWork). But I can’t be certain without seeing more of your code.

    Allocations in C++ don’t have a reference count, so you should delete the Work instance when you’re done using it. If you delete it beforehand, it’s gone: use that pointer and the results will be undefined (might work, probably won’t). If you don’t delete it, it will leak (as you can see).

    You should refactor your code to use smart pointers — look at std::unique_ptr<> and std::shared_ptr<> (or boost::scoped_ptr<> and boost::shared_ptr<>). They will help ensure things get deleted when they really should be:

    for(;;)
    {
        std::unique_ptr<int> uptr(new int);
        std::shared_ptr<int> sptr = std::make_shared<int>();
    
        // uptr and sptr will be automatically deleted at this end of scope.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void someCode() { char memory[sizeof(Foo)]; void* place = memory; Foo* f = new(place) Foo();
void return_input (void) { char array[30]; gets (array); printf(%s\n, array); } After compiling it
void reverse(char *str){ int i,j; char temp; for(i=0,j=strlen(str)-1; i<j; i++, j--){ temp = *(str
void distinct (void) { char sent; int n1, n2, n3, n4, n5, n6, n7;
void main(){ int i,k; char* p; int j; printf(address of i is %d \naddress
void GenerateSurvey(string AnketId, System.Web.UI.WebControls.PlaceHolder plch) { var db = new Xrm.XrmDataContext(Microsoft.Xrm.Client.CrmConnection.Parse(Utils.getXrmConnectionString(_PortalBrandHelper.BrandProxy.BrandDedicatedCrmOrgName))); var AnketSoru =
- (void)main { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Warning goes here NSRunLoop
I'm reading some GPS data (longitude,latitude) from a ServerManager class that stores it. The
void gctinp (char *inp, int siz) { puts (Input value: ); fgets (inp, siz,
void foo(char *p) { int i; int len = strlen(p); p = malloc(sizeof(char)*len+2); p[0]

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.