auto_ptr not dereferencable

Discussion in 'C++' started by mkarja, Feb 27, 2008.

  1. mkarja

    mkarja Guest

    Hi,

    I'm having a bit of a problem with auto_ptr.

    Here's a code snippet and some explanation to follow.

    Communicator.h
    -------- code start --------
    typedef OsCommunication osCom;

    private:
    std::auto_ptr<osCom> m_osCom;
    -------- code end --------


    Communicator.cpp
    -------- code start --------
    void Communicator::Initialize(void *ptr)
    {
    if(ptr != 0)
    g_ServerWorker = (ServerWorker*)ptr;
    else
    try
    {
    m_osCom->Initialize(this);
    }
    catch(...)
    {
    }
    }
    -------- code end --------

    There are obviously lots of other stuff on both files, but those are
    the essential
    parts I think.
    The OsCommunication is an abstract class that's why I use typedef and
    auto_ptr.
    If someone has better idea for that, I'm all ears.
    The "m_osCom->Initialize(this);" line is what causes the error when I
    debug it.
    The error is somthing like this "Debug Assertion Failed, auto_ptr not
    dereferencable"

    Any help would be greatly appriciated.

    ----
    mkarja
     
    mkarja, Feb 27, 2008
    #1
    1. Advertising

  2. mkarja

    online_wan Guest

    On 2ÔÂ27ÈÕ, ÏÂÎç6ʱ21·Ö, mkarja <> wrote:
    > Hi,
    >
    > I'm having a bit of a problem with auto_ptr.
    >
    > Here's a code snippet and some explanation to follow.
    >
    > Communicator.h
    > -------- code start --------
    > typedef OsCommunication osCom;
    >
    > private:
    > std::auto_ptr<osCom> m_osCom;
    > -------- code end --------
    >
    > Communicator.cpp
    > -------- code start --------
    > void Communicator::Initialize(void *ptr)
    > {
    > if(ptr != 0)
    > g_ServerWorker = (ServerWorker*)ptr;
    > else
    > try
    > {
    > m_osCom->Initialize(this);
    > }
    > catch(...)
    > {
    > }}
    >
    > -------- code end --------
    >
    > There are obviously lots of other stuff on both files, but those are
    > the essential
    > parts I think.
    > The OsCommunication is an abstract class that's why I use typedef and
    > auto_ptr.
    > If someone has better idea for that, I'm all ears.
    > The "m_osCom->Initialize(this);" line is what causes the error when I
    > debug it.
    > The error is somthing like this "Debug Assertion Failed, auto_ptr not
    > dereferencable"
    >
    > Any help would be greatly appriciated.
    >
    > ----
    > mkarja


    "Debug Assertion Failed, auto_ptr not
    dereferencable"

    please new one instance and is pointed by auto_ptr.
     
    online_wan, Feb 27, 2008
    #2
    1. Advertising

  3. mkarja

    mkarja Guest

    >
    > "Debug Assertion Failed, auto_ptr not
    > dereferencable"
    >
    > please new one instance and is pointed by auto_ptr.



    Thank you for the answer, but I'm sorry, I don't understand what you
    mean.
    Do you mean that I should do something like this:
    std::auto_ptr<osCom> m_osCom (new osCom);

    I've tried that, but it just gives me an error saying,
    "error C2059: syntax error : 'new'"

    ----
    mkarja
     
    mkarja, Feb 27, 2008
    #3
  4. mkarja

    mkarja Guest

    On 28 helmi, 07:00, raof01 <> wrote:
    > Hello mkarja,
    >
    > >> "Debug Assertion Failed, auto_ptr not
    > >> dereferencable"
    > >> please new one instance and is pointed by auto_ptr.

    >
    > > Thank you for the answer, but I'm sorry, I don't understand what you
    > > mean.
    > > Do you mean that I should do something like this:
    > > std::auto_ptr<osCom> m_osCom (new osCom);
    > > I've tried that, but it just gives me an error saying, "error C2059:
    > > syntax error : 'new'"

    >
    > > ----
    > > mkarja

    >
    > Forgot "()"? :)
    > std::auto_ptr<osCom> m_osCom (new osCom());
    > But I think you'd better initialize m_osCom in ctor.
    > BTW, are you coming from C# community or ?


    It gives the same syntax error even with
    std::auto_ptr<osCom> m_osCom (new osCom());

    Do I have to initialize it somehow in the .h or .cpp file before
    I try to use it in the .cpp file?

    No, I'm not coming from C#. Just never used auto_ptr before and
    are a bit lost with it, it seems :)

    ----
    mkarja
     
    mkarja, Feb 27, 2008
    #4
  5. mkarja

    mkarja Guest

    On 28 helmi, 07:23, raof01 <> wrote:
    > Hello mkarja,
    >
    >
    >
    > > On 28 helmi, 07:00, raof01 <> wrote:

    >
    > >> Hello mkarja,

    >
    > >>>> "Debug Assertion Failed, auto_ptr not
    > >>>> dereferencable"
    > >>>> please new one instance and is pointed by auto_ptr.
    > >>> Thank you for the answer, but I'm sorry, I don't understand what you
    > >>> mean.
    > >>> Do you mean that I should do something like this:
    > >>> std::auto_ptr<osCom> m_osCom (new osCom);
    > >>> I've tried that, but it just gives me an error saying, "error C2059:
    > >>> syntax error : 'new'"
    > >>> ----
    > >>> mkarja
    > >> Forgot "()"? :)
    > >> std::auto_ptr<osCom> m_osCom (new osCom());
    > >> But I think you'd better initialize m_osCom in ctor.
    > >> BTW, are you coming from C# community or ?

    > > It gives the same syntax error even with
    > > std::auto_ptr<osCom> m_osCom (new osCom());
    > > Do I have to initialize it somehow in the .h or .cpp file before I try
    > > to use it in the .cpp file?

    >
    > > No, I'm not coming from C#. Just never used auto_ptr before and are a
    > > bit lost with it, it seems :)

    >
    > > ----
    > > mkarja

    >
    > Sorry for confusing you. If osCom doesn't have ctor with no arguments, you
    > should use another ctor of osCom.
    > E.g.
    > You have:
    > class osCom
    > {
    > public: // All kinds of ctor's but without osCom()
    > osCom(int) { /* ... */ }
    > osCom(const string &) { /* ... */ }
    > // ...};
    >
    > Then you will use something like below:
    > std::auto_ptr<osCom> m_osCom (new osCom(0));
    > or
    > std::auto_ptr<osCom> m_osCom (new osCom("Hello"));
    >
    > If you want to initialize it elsewhere, put the initialization into initialization
    > list of ctor - just a recommendation.
    >
    > All my code can be compiled by g++ 3.4.4.
    >
    > WBR,
    > raof01
    > mailto:
    > ==================
    > Thoughts are but dreams till their effects be tried.


    No worries, I'm quite capable of confusing myself. I appreciate your
    efforts.
    I'll try to explain a bit better my case also.
    The "m_osCom->Initialize(this);" line in the Communicator.cpp calls an
    Initialize
    function in OsCommunication.cpp file. The Initialize is defined in
    OsCommunication.h
    file as void Initialize(void *);.
    The OsCommunication is an abstract class so I can't
    instantiate it straight. So I've done an "typedef OsCommunication
    osCom;" in the
    Communicator.h file and then define the m_osCom as
    std::auto_ptr<osCom> m_osCom; in
    the same Communicator.h file.
    Then when I get to the "m_osCom->Initialize(this);" line in the
    Communicator.cpp
    file, I get the error "auto_ptr not dereferencable".

    I hope this makes this a bit clearer.

    ----
    mkarja
     
    mkarja, Feb 27, 2008
    #5
  6. mkarja

    Old Wolf Guest

    On Feb 28, 3:29 am, mkarja <> wrote:
    > Communicator.h file and then define the m_osCom as
    > std::auto_ptr<osCom> m_osCom; in
    > the same Communicator.h file.
    > Then when I get to the "m_osCom->Initialize(this);" line in the
    > Communicator.cpp
    > file, I get the error "auto_ptr not dereferencable".


    m_osCom is a smart pointer that doesn't yet point
    anywhere. You can't go -> on it. It is the same
    error as this:

    string *ptr = NULL;
    ptr->resize(5);

    You will have to make it point to an instance of
    a class derived from osCom (since, you say, osCom is
    abstract), before you try to go -> on it.
     
    Old Wolf, Feb 27, 2008
    #6
  7. mkarja

    Fei Rao Guest

    Hello online_wan,

    Yes, as you mentioned, m_osCom must be initialized before it can be dereferenced.

    To supplement you with more details:
    std::auto_ptr<osCom> m_osCom;
    auto_prt(0) is called when create an object of Communicator is created.

    > On 2ÔÂ27ÈÕ, ÏÂÎç6ʱ21·Ö, mkarja <> wrote:
    >
    >> Hi,
    >>
    >> I'm having a bit of a problem with auto_ptr.
    >>
    >> Here's a code snippet and some explanation to follow.
    >>
    >> Communicator.h
    >> -------- code start --------
    >> typedef OsCommunication osCom;
    >> private:
    >> std::auto_ptr<osCom> m_osCom;
    >> -------- code end --------
    >> Communicator.cpp
    >> -------- code start --------
    >> void Communicator::Initialize(void *ptr)
    >> {
    >> if(ptr != 0)
    >> g_ServerWorker = (ServerWorker*)ptr;
    >> else
    >> try
    >> {
    >> m_osCom->Initialize(this);
    >> }
    >> catch(...)
    >> {
    >> }}
    >> -------- code end --------
    >>
    >> There are obviously lots of other stuff on both files, but those are
    >> the essential
    >> parts I think.
    >> The OsCommunication is an abstract class that's why I use typedef and
    >> auto_ptr.
    >> If someone has better idea for that, I'm all ears.
    >> The "m_osCom->Initialize(this);" line is what causes the error when I
    >> debug it.
    >> The error is somthing like this "Debug Assertion Failed, auto_ptr not
    >> dereferencable"
    >> Any help would be greatly appriciated.
    >>
    >> ----
    >> mkarja

    > "Debug Assertion Failed, auto_ptr not
    > dereferencable"
    > please new one instance and is pointed by auto_ptr.
    >
     
    Fei Rao, Feb 28, 2008
    #7
  8. mkarja

    raof01 Guest

    Hello mkarja,

    >> "Debug Assertion Failed, auto_ptr not
    >> dereferencable"
    >> please new one instance and is pointed by auto_ptr.
    >>

    > Thank you for the answer, but I'm sorry, I don't understand what you
    > mean.
    > Do you mean that I should do something like this:
    > std::auto_ptr<osCom> m_osCom (new osCom);
    > I've tried that, but it just gives me an error saying, "error C2059:
    > syntax error : 'new'"
    >
    > ----
    > mkarja


    Forgot "()"? :)
    std::auto_ptr<osCom> m_osCom (new osCom());
    But I think you'd better initialize m_osCom in ctor.
    BTW, are you coming from C# community or ?
     
    raof01, Feb 28, 2008
    #8
  9. mkarja

    online_wan Guest

    On Feb 27, 8:59 pm, mkarja <> wrote:
    > > "Debug Assertion Failed, auto_ptr not
    > > dereferencable"

    >
    > > please new  one instance and is pointed by auto_ptr.

    >
    > Thank you for the answer, but I'm sorry, I don't understand what you
    > mean.
    > Do you mean that I should do something like this:
    > std::auto_ptr<osCom> m_osCom (new osCom);
    >
    > I've tried that, but it just gives me an error saying,
    > "error C2059: syntax error : 'new'"
    >
    > ----
    > mkarja


    Sorry, my english is poor.

    I think, osCom's Constructor isn't public, so you can't created
    Instance by "new".

    auto_ptr must store one pointer, and before you use "->" operator like
    "m_osCom->Initialize(this);", the stored pointer must not be null.

    you can lookup the use method from stl reference manual.

    Sorry, my english is very poor, i can't say it clear.
     
    online_wan, Feb 28, 2008
    #9
  10. mkarja

    raof01 Guest

    Hello mkarja,

    > On 28 helmi, 07:00, raof01 <> wrote:
    >
    >> Hello mkarja,
    >>
    >>>> "Debug Assertion Failed, auto_ptr not
    >>>> dereferencable"
    >>>> please new one instance and is pointed by auto_ptr.
    >>> Thank you for the answer, but I'm sorry, I don't understand what you
    >>> mean.
    >>> Do you mean that I should do something like this:
    >>> std::auto_ptr<osCom> m_osCom (new osCom);
    >>> I've tried that, but it just gives me an error saying, "error C2059:
    >>> syntax error : 'new'"
    >>> ----
    >>> mkarja

    >> Forgot "()"? :)
    >> std::auto_ptr<osCom> m_osCom (new osCom());
    >> But I think you'd better initialize m_osCom in ctor.
    >> BTW, are you coming from C# community or ?

    > It gives the same syntax error even with
    > std::auto_ptr<osCom> m_osCom (new osCom());
    > Do I have to initialize it somehow in the .h or .cpp file before I try
    > to use it in the .cpp file?
    >
    > No, I'm not coming from C#. Just never used auto_ptr before and are a
    > bit lost with it, it seems :)
    >
    > ----
    > mkarja


    Sorry for confusing you. If osCom doesn't have ctor with no arguments, you
    should use another ctor of osCom.
    E.g.
    You have:
    class osCom
    {
    public: // All kinds of ctor's but without osCom()
    osCom(int) { /* ... */ }
    osCom(const string &) { /* ... */ }
    // ...
    };
    Then you will use something like below:
    std::auto_ptr<osCom> m_osCom (new osCom(0));
    or
    std::auto_ptr<osCom> m_osCom (new osCom("Hello"));

    If you want to initialize it elsewhere, put the initialization into initialization
    list of ctor - just a recommendation.

    All my code can be compiled by g++ 3.4.4.

    WBR,
    raof01
    mailto:
    ==================
    Thoughts are but dreams till their effects be tried.
     
    raof01, Feb 28, 2008
    #10
  11. In message
    <>,
    online_wan <> writes
    >On Feb 27, 8:59 pm, mkarja <> wrote:
    >> > "Debug Assertion Failed, auto_ptr not
    >> > dereferencable"

    >>
    >> > please new  one instance and is pointed by auto_ptr.

    >>
    >> Thank you for the answer, but I'm sorry, I don't understand what you
    >> mean.
    >> Do you mean that I should do something like this:
    >> std::auto_ptr<osCom> m_osCom (new osCom);
    >>
    >> I've tried that, but it just gives me an error saying,
    >> "error C2059: syntax error : 'new'"
    >>
    >> ----
    >> mkarja

    >
    >Sorry, my english is poor.


    Your C++ is worse.
    >
    >I think, osCom's Constructor isn't public, so you can't created
    >Instance by "new".


    Not the problem. The syntax error is because you can't initialize a
    member variable in its declaration, you have to do it in the
    constructor.
    >
    >auto_ptr must store one pointer, and before you use "->" operator like
    >"m_osCom->Initialize(this);", the stored pointer must not be null.
    >
    >you can lookup the use method from stl reference manual.
    >
    >Sorry, my english is very poor, i can't say it clear.


    --
    Richard Herring
     
    Richard Herring, Feb 28, 2008
    #11
  12. In message <99.com>, raof01
    <> writes
    >Hello mkarja,
    >
    >> On 28 helmi, 07:00, raof01 <> wrote:
    >>
    >>> Hello mkarja,
    >>>
    >>>>> "Debug Assertion Failed, auto_ptr not
    >>>>> dereferencable"
    >>>>> please new one instance and is pointed by auto_ptr.
    >>>> Thank you for the answer, but I'm sorry, I don't understand what you
    >>>> mean.
    >>>> Do you mean that I should do something like this:
    >>>> std::auto_ptr<osCom> m_osCom (new osCom);
    >>>> I've tried that, but it just gives me an error saying, "error C2059:
    >>>> syntax error : 'new'"
    >>>> mkarja
    >>> Forgot "()"? :)


    No.

    >>> std::auto_ptr<osCom> m_osCom (new osCom());


    That won't work either. With or without the () it's the syntax for
    defining and initialising a local variable in a function, not the syntax
    for defining a class member.

    >>> But I think you'd better initialize m_osCom in ctor.


    Not "better", _must_. And it's the constructor of _Communicator_ we're
    talking about.

    >>> BTW, are you coming from C# community or ?

    >> It gives the same syntax error even with
    >> std::auto_ptr<osCom> m_osCom (new osCom());
    >> Do I have to initialize it somehow in the .h or .cpp file before I try
    >> to use it in the .cpp file?


    You have to initialize it in the initializer list of Communicator's
    constructor(s).

    >> No, I'm not coming from C#. Just never used auto_ptr before and are
    >>
    >> bit lost with it, it seems :)


    Nothing specifically to do with auto_ptr - you'd have the same issues
    with a raw pointer.
    >
    >Sorry for confusing you. If osCom doesn't have ctor with no arguments,
    >you should use another ctor of osCom.


    The issue is not how to construct osCom, it's how to make Communicator's
    constructor initialise its member variable m_osCom.

    >E.g.
    >You have:
    >class osCom
    >{
    >public: // All kinds of ctor's but without osCom()
    > osCom(int) { /* ... */ }
    > osCom(const string &) { /* ... */ } // ...
    >};
    >Then you will use something like below:
    > std::auto_ptr<osCom> m_osCom (new osCom(0));
    >or
    > std::auto_ptr<osCom> m_osCom (new osCom("Hello"));


    In the body of a function, that declares a new local variable. In a
    class definition it's a syntax error. Either way it doesn't initialise
    the member variable.
    >
    >If you want to initialize it elsewhere, put the initialization into
    >initialization list of ctor - just a recommendation.


    If it's a class member, it's essential.

    Communicator::Communicator(/*...*/) : m_osCom(new osCom) { /*...*/ }

    You may need to pass additional arguments to osCom's constructor, too.
    If for some reason construction of the object has to be delayed, use
    auto_ptr's reset() function elsewhere in the code:

    m_osCom.reset(new osCom);
    >
    >All my code can be compiled by g++ 3.4.4.


    But it doesn't do what mkarja wants, so how does that help?

    --
    Richard Herring
     
    Richard Herring, Feb 28, 2008
    #12
  13. mkarja

    mkarja Guest

    On 27 helmi, 17:00, Old Wolf <> wrote:
    > On Feb 28, 3:29 am, mkarja <> wrote:
    >
    > > Communicator.h file and then define the m_osCom as
    > > std::auto_ptr<osCom> m_osCom; in
    > > the same Communicator.h file.
    > > Then when I get to the "m_osCom->Initialize(this);" line in the
    > > Communicator.cpp
    > > file, I get the error "auto_ptr not dereferencable".

    >
    > m_osCom is a smart pointer that doesn't yet point
    > anywhere. You can't go -> on it. It is the same
    > error as this:
    >
    > string *ptr = NULL;
    > ptr->resize(5);
    >
    > You will have to make it point to an instance of
    > a class derived from osCom (since, you say, osCom is
    > abstract), before you try to go -> on it.


    Ok, I think I got it working, or at least I got this thing
    working, of course there's another issue now, but that's
    a story for another time.
    Now I understand abstract classes a bit better.

    Thank you very much Old Wolf, much appreciated.
    Also thanks to online_wan, your english is comprehensible,
    so don't worry about that :)

    ----
    mkarja
     
    mkarja, Feb 28, 2008
    #13
  14. mkarja

    James Kanze Guest

    On Feb 28, 6:00 am, raof01 <> wrote:

    > >> "Debug Assertion Failed, auto_ptr not
    > >> dereferencable"
    > >> please new one instance and is pointed by auto_ptr.


    > > Thank you for the answer, but I'm sorry, I don't understand
    > > what you mean.
    > > Do you mean that I should do something like this:
    > > std::auto_ptr<osCom> m_osCom (new osCom);
    > > I've tried that, but it just gives me an error saying, "error C2059:
    > > syntax error : 'new'"


    > Forgot "()"? :)


    > std::auto_ptr<osCom> m_osCom (new osCom());


    That shouldn't change anything (supposing that osCom is a class
    type).

    > But I think you'd better initialize m_osCom in ctor.


    That's the problem, no doubt. m_osCom is a member, so the
    initialization should be in the initializer list of the
    constructor:

    Communicator::Communicator()
    : m_osCom( new osCom )
    {
    }


    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 28, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,584
    Dave Rahardja
    Jan 11, 2005
  2. David Bilsby
    Replies:
    5
    Views:
    2,094
    David Bilsby
    Oct 9, 2007
  3. astralboy79
    Replies:
    1
    Views:
    581
    astralboy79
    Jun 13, 2008
  4. Sousuke
    Replies:
    9
    Views:
    1,184
    Bart van Ingen Schenau
    Mar 16, 2010
  5. Jarek Blakarz
    Replies:
    2
    Views:
    260
    Balog Pal
    Jan 25, 2013
Loading...

Share This Page