使用智能指针时访问冲突

时间:2012-07-31 08:22:42

标签: c++ c++11 access-violation unique-ptr

obj-> ip =“127.0.0.1”; 有什么问题?我在这里遇到访问冲突。

我记得在课堂上 - 你可以使用私人成员,不是吗?

typedef struct Header
{
    SOCKET sock;
    SOCKADDR_IN addr;
    WORD WSAVersion;
    WSAData data;
    char *ip;
    u_short port;
    int result;
} Header;

class A
{
private:
    std::unique_ptr<Header> obj;

public:
    A(void)
    {
        obj(new Header);
        obj->ip = "127.0.0.1"; // here comes the error!
    }

    ~A(void)
    {
        obj.release();
    }
};

int main(void)
{
    std::unique_ptr<A> handler(new A);

2 个答案:

答案 0 :(得分:4)

构造函数体中的

obj(new Header);是荒谬的,并且你正在泄漏内存(完全违背了首先使用智能指针的目的) - 尝试以下方法:

class A
{
    std::unique_ptr<Header> obj;

public:
    A() : obj(new Header()) // value-initialize object, not default-initialize
    {
        obj->ip = "127.0.0.1";
    }
};

答案 1 :(得分:0)

感谢您的帮助,伙计们!我已经编辑了我的源代码,现在它确实可以正常工作而不会发生违规,并且它确实连接到远程主机:

#include <iostream>
#include <memory>
#include <WinSock2.h>
#pragma comment(lib, "Ws2_32.lib")
typedef struct Header
{
    SOCKET sock;
    SOCKADDR_IN addr;
    WORD WSAVersion;
    WSAData data;
    char *ip;
    u_short port;
    int result;
} Header;
class Connector
{
private:
    std::unique_ptr<Header> obj;
public:
    Connector(void)
    {
        obj.reset(new Header);
        obj->ip = "193.201.231.44";
        obj->port = 2775;
        obj->WSAVersion = 0x202;
        obj->addr.sin_family = AF_INET;
        obj->addr.sin_addr.s_addr = inet_addr(obj->ip);
        obj->addr.sin_port = htons(obj->port);
        this->MakeConnection(&obj);
    }
    ~Connector(void)
    {
        obj.release();
    }
    int MakeConnection(std::unique_ptr<Header> *pointer)
    {
        (*pointer)->result = WSAStartup((*pointer)->WSAVersion, &(*pointer)->data);
        if((*pointer)->result != NO_ERROR)
        {
            std::cout << "WSA failed to start with the error: " << std::endl << (*pointer)->result << std::endl;
            return 0;
        }
        else
        {
            std::cout << "WSA started successfully" << std::endl;
            (*pointer)->sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if((*pointer)->sock == SOCKET_ERROR)
            {
                std::cout << "Failed to init new socket, with the error: " << WSAGetLastError() << std::endl;
                return 0;
            }
            else
            {
                (*pointer)->result = connect(obj->sock, (sockaddr*)&(*pointer)->addr, sizeof((*pointer)->addr));
                if((*pointer)->result == SOCKET_ERROR)
                {
                    std::cout << "Failed to connect to the remote host " << (*pointer)->ip << ":" << (*pointer)->port
                        << ", with the error: " << WSAGetLastError() << std::endl;
                    return 0;
                }
                else
                {
                    std::cout << "Connected to the remote host " << (*pointer)->ip << ":" << (*pointer)->port << std::endl;
                    return 1;
                }
            }
        }
    }
};
void init(void)
{
    std::unique_ptr<Connector> obj;
    obj.reset(new Connector);
    obj.release();
}
int main(void)
{
    init();
    return 0;
}