My Funky Pass By Reference Issue

V

VirGin

I am passing an object by reference to a function that is supposed to
populate it with current data.
Both the destination object and the source have nested std::lists,
some branches of the lists might be empty.
The function crashes when copying the lists, the error info is:

..dlu!std::list<HWND__ *,std::allocator<HWND__ *> >::clear() Line 818
+ 0xf bytes C++
..dlu!std::list said:
::_Assign<std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> >(std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> _First=0xcdcdcdcd {unused=??? },
std::list<HWND__ *,std::allocator<HWND__ *> >::_Const_iterator<1>
_Last=0xcdcdcdcd {unused=??? }, std::input_iterator_tag
__formal={...}) Line 678 C++
..dlu!std::list said:
::assign<std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> >(std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> _First=0xcdcdcdcd {unused=??? },
std::list<HWND__ *,std::allocator<HWND__ *> >::_Const_iterator<1>
_Last=0xcdcdcdcd {unused=??? }) Line 666 C++
..dlu!std::list<HWND__ *,std::allocator<HWND__ *> >::eek:perator=(const
std::list<HWND__ *,std::allocator<HWND__ *> > & _Right=[0]()) Line
522 C++
..dlu!A::eek:perator=(const A & __that={...}) + 0xf2 bytes C++


void clear()
{ // erase all

#if _HAS_ITERATOR_DEBUGGING
this->_Orphan_ptr(*this, 0);
#endif /* _HAS_ITERATOR_DEBUGGING */

_Nodeptr _Pnext;
_Nodeptr _Pnode = _Nextnode(_Myhead);



The structure of the source and destination is roughly:


struct C
{
member
member
}

struct B
{
member
member
std::list<B>
std::list<C>
}

struct A
{
member
member
std::list<B>
}


std::list <A>
A::member
A::member
A::std::list<B>
B::member
B::member
B::std::list<B>
B::std::list<C>
C::member
C::member


// Function to return a copy of type A
void ClassName::projectCurrGet(UINT iVal, A *xProj)
{
std::list<A>::iterator ProjIter;
std::list<A>::size_type tProjSize;
A tProj;
A tProj2; //Test


ProjIter = xProjects.begin();
while(ProjIter != xProjects.end())
{
tProj = (*ProjIter);
if (iVal == tProj.TabNum)
{
// straight assignment test
tProj2 = tProj;

xProj = tProj; // This crashes on the embedded list
// xProj = &(tProj);
// *xProj = *ProjIter;
// *xProj = *(ProjIter);
// *xProj = (*ProjIter);
// This works, but all I'm doing is assigning the *local* 'copy' /
address to xProj. When the function ends, the stack is hosed and xProj
points at nothing.
// xProj = &(*ProjIter);

break;
}
// ZeroMemory(&tProj, sizeof(tProj));
ProjIter++;
}
return;
}


The Source is a member of ClassName:
A xProject;

The Destination is type A.

It is being called as so:
ProjectCurrGet(iTmp, &xProject);

It is only when the function assigns the copy from the std::list to
the target (which was passed by reference) that it fails.
The assignment to a local copy of A works without a problem.

My last idea is to overload the assignment operator of A. I'm hesitant
to do this but if that is my only option, I will.

Any suggestions?


-V-
 
J

John Harrison

VirGin said:
I am passing an object by reference to a function that is supposed to
populate it with current data.
Both the destination object and the source have nested std::lists,
some branches of the lists might be empty.
The function crashes when copying the lists, the error info is:

.dlu!std::list<HWND__ *,std::allocator<HWND__ *> >::clear() Line 818
+ 0xf bytes C++
.dlu!std::list said:
::_Assign<std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> >(std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> _First=0xcdcdcdcd {unused=??? },
std::list<HWND__ *,std::allocator<HWND__ *> >::_Const_iterator<1>
_Last=0xcdcdcdcd {unused=??? }, std::input_iterator_tag
__formal={...}) Line 678 C++
.dlu!std::list said:
::assign<std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> >(std::list<HWND__ *,std::allocator<HWND__ *>
::_Const_iterator<1> _First=0xcdcdcdcd {unused=??? },
std::list<HWND__ *,std::allocator<HWND__ *> >::_Const_iterator<1>
_Last=0xcdcdcdcd {unused=??? }) Line 666 C++
.dlu!std::list<HWND__ *,std::allocator<HWND__ *> >::eek:perator=(const
std::list<HWND__ *,std::allocator<HWND__ *> > & _Right=[0]()) Line
522 C++
.dlu!A::eek:perator=(const A & __that={...}) + 0xf2 bytes C++


void clear()
{ // erase all

#if _HAS_ITERATOR_DEBUGGING
this->_Orphan_ptr(*this, 0);
#endif /* _HAS_ITERATOR_DEBUGGING */

_Nodeptr _Pnext;
_Nodeptr _Pnode = _Nextnode(_Myhead);



The structure of the source and destination is roughly:


struct C
{
member
member
}

struct B
{
member
member
std::list<B>
std::list<C>
}

struct A
{
member
member
std::list<B>
}


std::list <A>
A::member
A::member
A::std::list<B>
B::member
B::member
B::std::list<B>
B::std::list<C>
C::member
C::member


// Function to return a copy of type A
void ClassName::projectCurrGet(UINT iVal, A *xProj)
{
std::list<A>::iterator ProjIter;
std::list<A>::size_type tProjSize;
A tProj;
A tProj2; //Test


ProjIter = xProjects.begin();
while(ProjIter != xProjects.end())
{
tProj = (*ProjIter);
if (iVal == tProj.TabNum)
{
// straight assignment test
tProj2 = tProj;

xProj = tProj; // This crashes on the embedded list
// xProj = &(tProj);
// *xProj = *ProjIter;
// *xProj = *(ProjIter);
// *xProj = (*ProjIter);
// This works, but all I'm doing is assigning the *local* 'copy' /
address to xProj. When the function ends, the stack is hosed and xProj
points at nothing.
// xProj = &(*ProjIter);

break;
}
// ZeroMemory(&tProj, sizeof(tProj));
ProjIter++;
}
return;
}


The Source is a member of ClassName:
A xProject;

The Destination is type A.

It is being called as so:
ProjectCurrGet(iTmp, &xProject);

It is only when the function assigns the copy from the std::list to
the target (which was passed by reference) that it fails.
The assignment to a local copy of A works without a problem.

My last idea is to overload the assignment operator of A. I'm hesitant
to do this but if that is my only option, I will.

Any suggestions?

Well of course you shouldn't make random changes when you don't
understand the cause of the error. You would just be digging a deeper
hole for yourself.

You seem to have tried every variation on assignment except the correct one

*xProj = tProj;

or is that just a typo in your post?

The other issue that strikes me is the various members you have in your
structs. What types are those? Do they have valid assignment operators
defined? The compiler generated assignment operator should never crash
if all the component parts are correctly assignable.

As usual with these kind of posts, the error is somewhere in the code
you didn't post. If the above doesn't help try posting a bit more. If
you can a small but complete program that demonstrates the error will
get you an answer in no time.

john
 
V

VirGin

Well of course you shouldn't make random changes when you don't
understand the cause of the error. You would just be digging a deeper
hole for yourself.

True. I'm sure you know how it is when one is tired and frustrated.
You seem to have tried every variation on assignment except the correct one

*xProj = tProj;

This fails during the copy of the list as well. This was the original
version.

or is that just a typo in your post?

I was in the middle of another attempt when I grabbed the code to
paste for a buddy.

The other issue that strikes me is the various members you have in your
structs. What types are those? Do they have valid assignment operators
defined? The compiler generated assignment operator should never crash
if all the component parts are correctly assignable.

They are basic types and TCHAR. The other members are copied without a
problem.
As usual with these kind of posts, the error is somewhere in the code
you didn't post. If the above doesn't help try posting a bit more. If
you can a small but complete program that demonstrates the error will
get you an answer in no time.

True, I agree.

After creating a small app just to test the functionality, I
discovered it was a call to ZeroMemory (ZeroMemory(&xProject,
sizeof(xProject));) before calling the function to populate the object
(ProjectCurrGet(iTmp, &xProject);).


Thank You for the Kick In The Head.


-V-
 
J

John Harrison

As usual with these kind of posts, the error is somewhere in the code
True, I agree.

After creating a small app just to test the functionality, I
discovered it was a call to ZeroMemory (ZeroMemory(&xProject,
sizeof(xProject));) before calling the function to populate the object
(ProjectCurrGet(iTmp, &xProject);).

Hey well done! Good though it is almost no-one posting here takes that
advice. Clearly you've got talent.

john
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top