user1544067
user1544067

Reputation: 1764

std::vector - error: cannot delete objects that are not pointers

Maybe someone can help me understand the error.

I write this code:

class Text
{
private:
    struct paragraph
    {
        vector<string> lines;       
    };

    vector<shared_ptr<paragraph>> paragraphs; 
public:

    Text()
    {
        paragraphs.push_back(shared_ptr<paragraph>(new paragraph()));
    }
};

int main()
{
    shared_ptr<Text> pText(nullptr);
    Text text();
    pText.reset(&text);
    return 0;
}

When I try to run it
I got this error:

    1>c:\program files\microsoft visual studio 10.0\vc\include\memory(1664): error C2541: 'delete' : cannot delete objects that are not pointers
1>          c:\program files\microsoft visual studio 10.0\vc\include\memory(1431) : see reference to function template instantiation 'void std::tr1::shared_ptr<_Ty>::_Resetp<_Ux>(_Ux (__cdecl *))' being compiled
1>          with
1>          [
1>              _Ty=Text,
1>              _Ux=Text (void)
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\memory(1607) : see reference to function template instantiation 'std::tr1::shared_ptr<_Ty>::shared_ptr<_Ux>(_Ux (__cdecl *))' being compiled
1>          with
1>          [
1>              _Ty=Text,
1>              _Ux=Text (void)
1>          ]
1>          c:\documents and settings\owner\שולחן העבודה\e\class.cpp(29) : see reference to function template instantiation 'void std::tr1::shared_ptr<_Ty>::reset<Text(void)>(_Ux (__cdecl *))' being compiled
1>          with
1>          [
1>              _Ty=Text,
1>              _Ux=Text (void)
1>          ]

What is meant "cannot delete objects that are not pointers"?
I'm not trying to delete any object.

Upvotes: 1

Views: 2431

Answers (3)

Andy Prowl
Andy Prowl

Reputation: 126412

The line Text text(); does not do what you think it does.

It parses it as the declaration of a function named text which accepts no argument and returns a value of type Text.

This is the reason why your line pText.reset(&text); does not compile.

However, you really do not want that line to compile: you are associating a shared_ptr object to a value with automatic storage duration: when the shared_ptr will go out of scope, it will try to delete that object, resulting in Undefined Behavior (most likely a crash in this case).

Upvotes: 2

Alex Chamberlain
Alex Chamberlain

Reputation: 4207

You main function should read.

int main()
{
    shared_ptr<Text> pText(new Text);
    return 0;
}

You had 2 problems. First, Text text() was being parsed as a function declaration. Second, you were passing the address of a stack variable to a shared_ptr, which will delete the object when the reference count reaches 0.

You should also consider whether you need to use a shared_ptr. Will you ever be sharing this pointer with any body else, or do you simply want to ensure it is destructed properly? You could consider unique_ptr in the latter case. Do you even need a pointer at all, could you just allocate the object on the stack?

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545518

In addition to the most vexing parse, your code contains a fundamental flaw:

You must not assign a pointer to a stack-allocated object to a shared_ptr.

This code will cause undefined behaviour which in practice means lots of pain:

shared_ptr<Text> pText(nullptr);
Text text;
pText.reset(&text);

shared_ptr will try to delete &text at the end of its lifetime.

Upvotes: 3

Related Questions