Home > Enterprise >  Segmentation fault using unique ptr
Segmentation fault using unique ptr

Time:12-11

I am trying to implement a simple Abstract Factory Design Pattern using unique pointers in C . I am following this link as reference (but modified a bit). The link uses raw pointers and I want to use unique ptrs instead. Please find the code below. Problem: Segmentation fault. I can't point my finger where this fault is arising from.

devices.hpp:

#ifndef PHONE_HPP
#define PHONE_HPP

#include <iostream>
#include <string>

class phone
{
    public:
        virtual std::string get_name() const = 0;
};

class iphone : public phone
{
    public:
        std::string get_name() const override;
};

class android : public phone
{
    public:
        std::string get_name() const override;
};

class laptop
{
    public:
        virtual std::string get_name() const = 0;
};

class mac : public laptop
{
    public:
        std::string get_name() const override;
};

class chromebook : public laptop
{
    public:
        std::string get_name() const override;
};

devices.cpp

#include <iostream>
#include <string>
#include "phone.hpp"

std::string iphone::get_name() const {
    return "iPhone X";
}

std::string android::get_name() const {
    return "Pixel 6";
}

std::string mac::get_name() const {
    return "Macbook Air";
}

std::string chromebook::get_name() const {
    return "Chrome Flex";
}

device_manufacturer.hpp file:


#ifndef DEVICE_MANUFACTURER_HPP
#define DEVICE_MANUFACTURER_HPP

#include <iostream>
#include <string>
#include <memory>
#include "phone.hpp"

class device_manufacturer
{
    public:
        enum Manufacturer{APPLE, GOOGLE};

        virtual std::unique_ptr<phone> get_phone() const = 0;
        virtual std::unique_ptr<laptop> get_laptop() const = 0;

        static std::unique_ptr<device_manufacturer> create_device(const Manufacturer& manf_input);
};

class Apple : public device_manufacturer
{
public:
    std::unique_ptr<phone> get_phone() const override;
    std::unique_ptr<laptop> get_laptop() const override;
};

class Google : public device_manufacturer
{
public:
    std::unique_ptr<phone> get_phone() const override;
    std::unique_ptr<laptop> get_laptop() const override;
};

device_manufacturer.cpp:

#include <iostream>
#include <string>
#include <memory>
#include "device_manufacturer.hpp"
#include "phone.hpp"

std::unique_ptr<device_manufacturer> device_manufacturer::create_device(const Manufacturer& manf_input){
    std::unique_ptr<device_manufacturer> manf_ptr;
    if (manf_input == Manufacturer::APPLE)
    {
        manf_ptr = std::make_unique<Apple>();
    }
    if (manf_input == Manufacturer::GOOGLE)
    {
        manf_ptr = std::make_unique<Google>();
    }
    return manf_ptr;
    
}

std::unique_ptr<phone> Apple::get_phone() const {
    return std::unique_ptr<iphone>();
}

std::unique_ptr<phone> Google::get_phone() const {
    return std::unique_ptr<android>();
}

std::unique_ptr<laptop> Apple::get_laptop() const {
    return std::unique_ptr<mac>();
}

std::unique_ptr<laptop> Google::get_laptop() const {
    return std::unique_ptr<chromebook>();
}

And finally the client code:

main.cpp:

#include <iostream>
#include <string>
#include <memory>
#include "device_manufacturer.hpp"
#include "phone.hpp"

int main(){
    std::unique_ptr<device_manufacturer> manf = device_manufacturer::create_device(device_manufacturer::Manufacturer::APPLE);
    std::cout << manf->get_laptop()->get_name() << std::endl;


    return 0;
}

CodePudding user response:

return std::unique_ptr<mac>() (and the others) does not allocate memory; it just says you have a unique_ptr which points to a mac; but it's actually initialized with a nullptr.

Change instead of return std::make_unique<mac>(); (and ditto for the other 3 functions)

  • Related