#Object gets cloned when passed by reference 2 times?

1 messages · Page 1 of 1 (latest)

stuck wolf
#

I have networking application which sends data, it consists of executable and multiple DLLs which also add some data to Buffer class. class Client contains Buffer as member object buffer, when data need to be sent it first calls Client::pack for itself and then for DLLs(shown as DLL::pack in code), I always pass buffer as reference. When I call DLL::pack with this->buffer argument it works without a problem, but once I use only buffer which was passed by reference from Client::send function so I assume it's same as this->buffer, the DLL somehow creates new copy of buffer which is independent of what I passed from Client::pack so anything I do DLL doesn't show in Client, the DLL actually makes new buffer initialized to default values so it's not even a copy of what I passed in the function and to make it even weirder the object which "cloned" doesn't even get automatically destroyed after DLL::pack function ends. When I print number of bytes in buffer on beginning and end of DLL::pack function, I would except something like: "0 4 0 4 0 4 0 4" but I get this instead: "0 4 4 8 8 12 12 16" so it just stays in memory. Does anyone know why would this "cloning" happen?

//EXE code
void Client::send()
{
  this->pack(this->buffer);
}

void Client::pack(Buffer& buffer)
{
  DLL::pack(buffer); //DLL creates own copy of buffer
  //DLL::pack(this->buffer) works when uncommented
}

//DLL code
void DLL:pack(Buffer& buffer)
{
  //fill buffer with data
}
mighty patioBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

lethal dagger
#

^ more code please

stuck wolf
#

oh I'm so stupid, how couldn't I see that... I tried to make reproducible example, found it worked fine, then looked at my code again to make sure I did it exactly same in the example, then noticed I passed recieve buffer instead of send buffer so this explains everything

#

!solved

mighty patioBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

trim widget
stuck wolf
#

probably not because I hear about rule of 3 for first time 😅 I'll look at it

trim widget
#

manually implemented

stuck wolf
#

I don't have copy constructor and assignment operator, why are you asking?

trim widget
stuck wolf
#

thank you for warning