Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello, I have a question. After creating a socket using createsocket, do I need to release it when closing the program? #132

Open
downloadproject opened this issue Oct 28, 2022 · 8 comments

Comments

@downloadproject
Copy link

downloadproject commented Oct 28, 2022

    QWeakPointer<MainWindow> weakSelf = QWeakPointer<MainWindow>(sharedFromThis());
    sockRecv  = Socket::createSocket();//Create a UDP data receiving port
    sockSend  = Socket::createSocket();//Create a UDP data sending port

    sockRecv->bindUdpSock(9001);//Bind UDP to port 9001 for receiving
    sockSend->bindUdpSock(0, "0.0.0.0");//Bind UDP to a random port for sending

    sockRecv->setOnRead([weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int){
            //Callback when data is received
           // DebugL << "recv data form " << getIP(addr) << ":" << buf->data();
        auto strongSelf = weakSelf.lock();
        if(!strongSelf) {
            return ;
        }
        strongSelf->handleOneRtp((uint8_t *) buf->data(), buf->size());
    });


I created it, and when I close the program, I get the following error:
the inferior stopped because it received a signal from operating system

Signal name :SIGSEGV

QWeakPointer weakSelf = QWeakPointer(sharedFromThis());
sockRecv = Socket::createSocket();//创建一个UDP数据接收端口
sockSend = Socket::createSocket();//创建一个UDP数据发送端口

sockRecv->bindUdpSock(9001);//接收UDP绑定9001端口
sockSend->bindUdpSock(0, "0.0.0.0");//发送UDP随机端口

sockRecv->setOnRead([weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int){
        //接收到数据回调
       // DebugL << "recv data form " << getIP(addr) << ":" << buf->data();
    auto strongSelf = weakSelf.lock();
    if(!strongSelf) {
        return ;
    }
    strongSelf->handleOneRtp((uint8_t *) buf->data(), buf->size());
});

我创建了以后,关闭程序的时候会报如下错误,
the inferior stopped because it received a signal from operating system

Signal name :SIGSEGV

TRANS_BY_GITHUB_AI_ASSISTANT

@xia-chu
Copy link
Member

xia-chu commented Oct 28, 2022

Your code looks fine.

你这个代码看起来没什么问题

TRANS_BY_GITHUB_AI_ASSISTANT

@downloadproject
Copy link
Author

downloadproject commented Oct 31, 2022

Hello, I am using Qt6. I only wrote one line of code in the entire project: Socket::Ptr sockRecv = Socket::createSocket(); // Create a UDP data receiving port. When I close the program in debug mode, the program crashes. I would appreciate any guidance. Thank you!

您好,我用qt6,整个工程就写了一条语句Socket::Ptr sockRecv = Socket::createSocket();//创建一个UDP数据接收端口,在调试状态关闭程序,程序会崩溃,恳请指教,谢谢!

TRANS_BY_GITHUB_AI_ASSISTANT

@wasphin
Copy link
Member

wasphin commented Nov 1, 2022

Can you take a look at the stack information?

可以看下栈信息.

TRANS_BY_GITHUB_AI_ASSISTANT

@downloadproject
Copy link
Author

downloadproject commented Nov 10, 2022

I'm so sorry to bother you. When I was debugging your test_tcpClient, I commented out a few lines of code related to the semaphore at the end. The program throws an error in debug mode. Could you please give me some guidance? Thank you!

int main() {
    // Set up the logging system
    Logger::Instance().add(std::make_shared<ConsoleChannel>());
    Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());

    TestClient::Ptr client(new TestClient());// Must use smart pointers
    client->startConnect("127.0.0.1",9000);// Connect to the server

    TcpClientWithSSL<TestClient>::Ptr clientSSL(new TcpClientWithSSL<TestClient>());// Must use smart pointers
    clientSSL->startConnect("127.0.0.1",9001);// Connect to the server

    // Exit program event handling
    //static semaphore sem;
   // signal(SIGINT, [](int) { sem.post(); });// Set exit signal
    //sem.wait();
    return 0;
}

实在不好意思,打扰了,我调试您的test_tcpClient时,注释了最后的semaphore几条语句 ,在debug下,程序会报错,恳请指教!谢谢!

int main() {
// 设置日志系统
Logger::Instance().add(std::make_shared());
Logger::Instance().setWriter(std::make_shared());

TestClient::Ptr client(new TestClient());//必须使用智能指针
client->startConnect("127.0.0.1",9000);//连接服务器

TcpClientWithSSL<TestClient>::Ptr clientSSL(new TcpClientWithSSL<TestClient>());//必须使用智能指针
clientSSL->startConnect("127.0.0.1",9001);//连接服务器

//退出程序事件处理
//static semaphore sem;

// signal(SIGINT, { sem.post(); });// 设置退出信号
//sem.wait();
return 0;
}

TRANS_BY_GITHUB_AI_ASSISTANT

@downloadproject
Copy link
Author

downloadproject commented Nov 10, 2022

Hello, when debugging test_udpSock, I changed while(!exitProgram) to if(!exitProgram). In debug mode, the program still crashes.

the inferior stopped because it received a signal from operating system

Signal name :SIGSEGV

您好,调试test_udpSock时,我将while(!exitProgram) 改成 if(!exitProgram) 在debug下,程序也会崩溃。

the inferior stopped because it received a signal from operating system

Signal name :SIGSEGV

TRANS_BY_GITHUB_AI_ASSISTANT

@downloadproject
Copy link
Author

downloadproject commented Nov 15, 2022

Excuse me, could you take a look at this? Thanks!!

不好意思,能帮我看下么?谢谢!!

TRANS_BY_GITHUB_AI_ASSISTANT

@wasphin
Copy link
Member

wasphin commented Nov 16, 2022

您好,调试test_udpSock时,我将while(!exitProgram) 改成 if(!exitProgram) 在debug下,程序也会崩溃。

the inferior stopped because it received a signal from operating system

Signal name :SIGSEGV

This test also failed to reproduce. You can use gdb to debug and see.

您好,调试test_udpSock时,我将while(!exitProgram) 改成 if(!exitProgram) 在debug下,程序也会崩溃。

the inferior stopped because it received a signal from operating system

Signal name :SIGSEGV

这个测试也没能复现。你可以用 gdb 调试下看看。

TRANS_BY_GITHUB_AI_ASSISTANT

@downloadproject
Copy link
Author

downloadproject commented Nov 17, 2022

Okay, thank you!

好的,谢谢!

TRANS_BY_GITHUB_AI_ASSISTANT

@alexliyu7352 alexliyu7352 changed the title 您好,请教一个问题createsocket 创建后,关闭程序时需要释放么 Hello, I have a question. After creating a socket using createsocket, do I need to release it when closing the program? Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants