Hi

I think im dealing with a possible bug using vector objects.

Specificaly a vector of CComPtr(s) related objects that contain even sinks.
Lets say i got a ATL class that handles events from a COM object
using IDispEventImpl<,,,>

----------------------------------------------------------------------------
------

class CSomeClass : public IDispEventImpl<,,,>
{
public:
CSomeClass (){}
~CSomeClass (){}

CComPtr<ISomeComObject> m_pSome;

BOOL Create()
{
m_pSome.CoCreateInstance(,,,);
return TRUE;
}

BEGIN_SINK_MAP(CSomeClass )
SINK_ENTRY_EX(0, DIID__ISomeComObjectEvents , 0x01, OnLoad)
END_SINK_MAP()

VOID __stdcall OnLoad(){}
};
----------------------------------------------------------------------------
------

then if do this :

vector<CSomeClass> m_vSome;
m_vSome.resize(2);
m_vSome[0].Create();
m_vSome[0].DispEventAdvise(m_vSome[0].m_pSome);
m_vSome[1].Create();
m_vSome[1].DispEventAdvise(m_vSome[1].m_pSome);
m_vSome.clear();

No problem, everything is fine and dandy!
BUT if i do this >> ( 2 resize operations)

vector<CSomeClass> m_vSome;
m_vSome.resize(1);
m_vSome[0].Create();
m_vSome[0].DispEventAdvise(m_vSome[0].m_pSome);
m_vSome.resize(2);
m_vSome[1].Create();
m_vSome[1].DispEventAdvise(m_vSome[1].m_pSome);
m_vSome.clear();

CRASH BANG! , , the event connections are lost during the "second" resize
operation.
So i had to do this to get things to Work :
...
for(i=0; i<m_vSome.size(); i++)
HRESULT hr = m_vSome[i].DispEventUnadvise(m_vSome[i].m_pSome);

UINT size = m_vSome.size();
m_vSome.resize(size+1);
m_vSome[size].Create();

for(i=0; i<m_vSome.size(); i++)
HRESULT hr = m_vSome[i].DispEventAdvise(m_vSome[i].m_pSome);
...

And now everything works..

Anybody ever had this issue before ?

Re: vector::resize bug by Igor

Igor
Fri Feb 06 16:29:05 CST 2004

"a.m.a" <amarcaurele@videotron.ca> wrote in message
news:hpUUb.47083$qy1.860284@wagner.videotron.net...
> I think im dealing with a possible bug using vector objects.
>
> Specificaly a vector of CComPtr(s) related objects that contain even
sinks.
> Lets say i got a ATL class that handles events from a COM object
> using IDispEventImpl<,,,>
>
> then if do this :
>
> vector<CSomeClass> m_vSome;

That's not a vector of CComPtr's.

> No problem, everything is fine and dandy!
> BUT if i do this >> ( 2 resize operations)
>
> vector<CSomeClass> m_vSome;
> m_vSome.resize(1);
> m_vSome[0].Create();
> m_vSome[0].DispEventAdvise(m_vSome[0].m_pSome);
> m_vSome.resize(2);
> m_vSome[1].Create();
> m_vSome[1].DispEventAdvise(m_vSome[1].m_pSome);
> m_vSome.clear();
>
> CRASH BANG! , , the event connections are lost during the "second"
resize
> operation.

But of course. vector requires its elements to be copy-constructible and
assignable. Essentially, a copy of the element is supposed to behave
identically to the original (value semantics). However, your class does
not have meaningful copy semantics: if an instance is attached to the
source, a copy of this instance is not. Your design is fundamentally
flawed.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Re: vector::resize bug by a

a
Fri Feb 06 17:39:55 CST 2004


Then can you please tel me what would be required to "add" to the class
CSomeClass to make it : "copy-constructible " so the vector can assign
all items properly ?

thanks again





Re: vector::resize bug by Igor

Igor
Fri Feb 06 17:41:53 CST 2004

"a.m.a" <amarcaurele@videotron.ca> wrote in message
news:_yVUb.48563$qy1.911537@wagner.videotron.net...
>
> Then can you please tel me what would be required to "add" to the
class
> CSomeClass to make it : "copy-constructible " so the vector can assign
> all items properly ?

Your class, by its nature, does not have value semantics. It does not
make sense to store it in vector. Allocate instances on the heap, store
pointers (possibly smart pointers) in vector.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Re: vector::resize bug by a

a
Fri Feb 06 18:11:46 CST 2004


"Igor Tandetnik" wrote :

> Your class, by its nature, does not have value semantics

im not sure what you mean but i know we've been in another post
about a similar subject. And i think what i need to do is add
a copy constructor ? If that's the case, then i would realy like to
know what that would be and what it would look like in class "CSomeClass"
...

Also, creating instances of CSomeClass and storing them in vector
will require vector::push_back() wich will cause the exact same
errors with event connection points as i would get with vector::resize(..)
( i know this because i tested both)

Do i even make sense ? :)

thanks a lot Igor. You are always a big help. :)










Re: vector::resize bug by TT

TT
Sat Feb 07 05:47:46 CST 2004

"a.m.a" <amarcaurele@videotron.ca> wrote in message
news:T0WUb.49049$qy1.933445@wagner.videotron.net...
>
> "Igor Tandetnik" wrote :
>
> > Your class, by its nature, does not have value semantics
>
> im not sure what you mean but i know we've been in another post
> about a similar subject. And i think what i need to do is add
> a copy constructor ? If that's the case, then i would realy like to
> know what that would be and what it would look like in class "CSomeClass"
> ...

You must ask yourself whether your class is really copy constructible, and
if copy construction won't pose an efficiency problem.

> Also, creating instances of CSomeClass and storing them in vector
> will require vector::push_back() wich will cause the exact same
> errors with event connection points as i would get with vector::resize(..)
> ( i know this because i tested both)

Because vector::push_back uses copy construction.

> Do i even make sense ? :)

Whatever. I don't think that your objects can be copyconstructible because
of performance issues. You could store pointers in the vector. Remember that
you still have to delete them before your vector is destroyed.

Tom.



Re: vector::resize bug by a

a
Sat Feb 07 13:58:00 CST 2004


"TT (Tom Tempelaere)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> wrote in
message news:Cf4Vb.2219$mZ7.240711@phobos.telenet-ops.be...
> "a.m.a" <amarcaurele@videotron.ca> wrote in message
> news:T0WUb.49049$qy1.933445@wagner.videotron.net...
> >
> > "Igor Tandetnik" wrote :
> >
> > > Your class, by its nature, does not have value semantics
> >
> > im not sure what you mean but i know we've been in another post
> > about a similar subject. And i think what i need to do is add
> > a copy constructor ? If that's the case, then i would realy like to
> > know what that would be and what it would look like in class
"CSomeClass"
> > ...
>
> You must ask yourself whether your class is really copy constructible, and
> if copy construction won't pose an efficiency problem.
>
> > Also, creating instances of CSomeClass and storing them in vector
> > will require vector::push_back() wich will cause the exact same
> > errors with event connection points as i would get with
vector::resize(..)
> > ( i know this because i tested both)
>
> Because vector::push_back uses copy construction.
>
> > Do i even make sense ? :)
>
> Whatever. I don't think that your objects can be copyconstructible because
> of performance issues. You could store pointers in the vector. Remember
that
> you still have to delete them before your vector is destroyed.
>
> Tom.
>
>

Well for now, unadvising and re-advising the events every time i resize the
vector
seems to work , no problems. Should i worry about any inpending issues if i
DONT
make the class copy constructible ?

thanks.




Re: vector::resize bug by TT

TT
Sat Feb 07 14:00:02 CST 2004

"a.m.a" <amarcaurele@videotron.ca> wrote in message
news:XobVb.62846$qy1.1503060@wagner.videotron.net...
>
> "TT (Tom Tempelaere)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> wrote in
> message news:Cf4Vb.2219$mZ7.240711@phobos.telenet-ops.be...
> > "a.m.a" <amarcaurele@videotron.ca> wrote in message
> > news:T0WUb.49049$qy1.933445@wagner.videotron.net...
> > >
> > > "Igor Tandetnik" wrote :
> > >
> > > > Your class, by its nature, does not have value semantics
> > >
> > > im not sure what you mean but i know we've been in another post
> > > about a similar subject. And i think what i need to do is add
> > > a copy constructor ? If that's the case, then i would realy like to
> > > know what that would be and what it would look like in class
> "CSomeClass"
> > > ...
> >
> > You must ask yourself whether your class is really copy constructible,
and
> > if copy construction won't pose an efficiency problem.
> >
> > > Also, creating instances of CSomeClass and storing them in vector
> > > will require vector::push_back() wich will cause the exact same
> > > errors with event connection points as i would get with
> vector::resize(..)
> > > ( i know this because i tested both)
> >
> > Because vector::push_back uses copy construction.
> >
> > > Do i even make sense ? :)
> >
> > Whatever. I don't think that your objects can be copyconstructible
because
> > of performance issues. You could store pointers in the vector. Remember
> that
> > you still have to delete them before your vector is destroyed.
> >
> > Tom.
> >
> >
>
> Well for now, unadvising and re-advising the events every time i resize
the
> vector
> seems to work , no problems. Should i worry about any inpending issues if
i
> DONT
> make the class copy constructible ?
>
> thanks.

Either you don't and use the pointers, or you make copy constructors. Vector
requires stored objects to be copy-constructible (amongst others).

You just take a risk if you don't make the copy constructor. You do a work
around that may break.

Tom.



Re: vector::resize bug by a

a
Sun Feb 08 17:54:43 CST 2004

thank you Tom, your tips are appreaciated