By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

My Funky Pass By Reference Issue

P: n/a
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<HWND__ *,std::allocator<HWND__ *>
>::_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<HWND__ *,std::allocator<HWND__ *>
>::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__ *::operator=(const
std::list<HWND__ *,std::allocator<HWND__ * & _Right=[0]()) Line
522 C++
..dlu!A::operator=(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-

Jun 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
VirGin wrote:
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<HWND__ *,std::allocator<HWND__ *>
>::_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<HWND__ *,std::allocator<HWND__ *>
>::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__ *::operator=(const
std::list<HWND__ *,std::allocator<HWND__ * & _Right=[0]()) Line
522 C++
.dlu!A::operator=(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
Jun 17 '07 #2

P: n/a
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-

Jun 17 '07 #3

P: n/a
>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);).
Hey well done! Good though it is almost no-one posting here takes that
advice. Clearly you've got talent.

john
Jun 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.