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

  • Home
  • SEARCH
  • 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 6744997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:09:04+00:00 2026-05-26T12:09:04+00:00

Please help me understand what is wrong with my code. Header File typedef void

  • 0

Please help me understand what is wrong with my code.

Header File

typedef void (*pStatusCallback)(UINT code, const CString& message);

class CComunicator
{
private:
    CUT_WSClient _client;
    bool _shouldTerminate;
    CString _serverAddress;
    UINT _serverPort;
    pStatusCallback _statusCallback;

    UINT _ThreadFunc();
    static UINT ThreadFunc(LPVOID pParam);
public:
    CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback);
    ~CComunicator(void);

    void Start();
    void Stop();
}

Source file

CComunicator::CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback) 
{
    _serverAddress = serverAddress;
    _serverPort = serverPort;
    _statusCallback = statusCallback;
}

CComunicator::~CComunicator(void)
{
}

void CComunicator::Start()
{
    _shouldTerminate = false;
    AfxBeginThread(CComunicator::ThreadFunc, this);
}

void CComunicator::Stop()
{
    _shouldTerminate = true;
}

UINT CComunicator::ThreadFunc(LPVOID pParam)
{
    return ((CComunicator*)pParam)->_ThreadFunc(); 
}

UINT CComunicator::_ThreadFunc()
{
    _statusCallback(0, _T("Connecting..."));
    _client.Connect(_serverPort, _serverAddress);
    _statusCallback(0, _T("Connected"));

    // do here some work


    _client.CloseConnection();

    return 0;
}

Usage

CComunicator com(_T("10.1.1.105"), 4502, ComunicatorCallback);
com.Start();

Why in method _ThreadFunc the _serverAddress contains garbage symbols? _serverPort has the correct value?
Nobody else is altering the _serverAddress.

Thx for any ideas.

  • 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-26T12:09:04+00:00Added an answer on May 26, 2026 at 12:09 pm

    The bug is in the code you didn’t paste, right after com.Start();. For example, if that function returns, com goes out of scope, taking the string out of scope too. Instead, do this:

    CComunicator *com=NEW CComunicator(_T("10.1.1.105"), 4502, CommunicatorCallback);
    
    com->Start();
    

    This will cause a memory leak, so you need to delete com; when you’re done with it. The best way might be like this:

    UINT CComunicator::ThreadFunc(LPVOID pParam)
    {
        UNIT ret = ((CComunicator*)pParam)->_ThreadFunc(); 
        delete (CComunicator *)pParam;
        return ret;
    }
    

    Other possibilities include keeping the object in scope until you’re sure the thread has terminated (if the thread that created the object can remain in that same scope until the object’s thread is done) and reference counting the object, where the object’s thread holds a reference to the object.

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

Sidebar

Related Questions

Please, help me understand, what's wrong with this code. (I am trying to build
please i didn't understand what wrong whith my code : float lng = [[stationEnCours
Please help me understand how the process goes. I understand that web browsers contain
I cannot understand how this is possible. Please help!! I have an app with
Please help me convert this line to C#. objManagementBaseObject.SetPropertyValue(hDefKey, CType(&H & Hex(RegistryHive.LocalMachine), Long)) Related
Please help me understand this. Here you can see that I have PYTHONPATH set
Please help! I'm really at my wits' end. My program is a little personal
Please help! Background info I have a WPF application which accesses a SQL Server
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag
Please help, I am stuck here --- irb> a = line of text\n line

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.