auto_ptr not dereferencable

M

mkarja

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.
 
O

online_wan

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.

"Debug Assertion Failed, auto_ptr not
dereferencable"

please new one instance and is pointed by auto_ptr.
 
M

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'"
 
M

mkarja

Hello 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 :)
 
M

mkarja

Hello 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:[email protected]
==================
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.
 
O

Old Wolf

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.
 
F

Fei Rao

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.
 
R

raof01

Hello mkarja,
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());
But I think you'd better initialize m_osCom in ctor.
BTW, are you coming from C# community or ?
 
O

online_wan

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'"

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.
 
R

raof01

Hello mkarja,
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 :)

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:[email protected]
==================
Thoughts are but dreams till their effects be tried.
 
R

Richard Herring

In message
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.
 
R

Richard Herring

raof01 said:
Hello mkarja,

No.

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.

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

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

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?
 
M

mkarja

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 :)
 
J

James Kanze

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 )
{
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top