So, lately I've been banging my head on this runtime error I get running this code. This is a client/server application with the client being a DLL injected into another process address space and the server a classical standalone program. This is the incriminated bit of code (I've removed non-relevant parts of the file):
#include "TableInfo.h"
TableInfo::TableInfo(tstring id) {
this->id = id;
}
TableInfo::TableInfo(int num, tstring id) {
//this->num = num;
this->id = id;
}
void TableInfo::dump() {
//tcout << "num = " << this->num << endl;
tcout << "id = " << this->id << endl;
}
As you can see I've got two constructors but they are pratically the same except for the signature. TableInfo
should contains some information that will be later embedded in another object which I'm trying to send from the client to the server over a TPC socket. This is how I do it:
TableInfo tt(999, L"MEGAOHM"); // WORKS!
//TableInfo tt(L"MEGAHOM2"); // DOESN'T WORK!
IpcRequest rr(CBE_NEW_TABLE);
rr.setTableInfo(tt);
c.sendRq(rr);
So, the IpcRequest
class is just another class containing one instance of TableInfo
and some other variables and it will get sent over to the server. The sendRq
function sends the entire object to the server where I try to display the data contained in it. As you can see from the code if I call the 2 arguments constructor it work otherwise I get a memory access violation error. According the to debugger the error occurs in the dump
function inside TableInfo
when I try to access the id field.
I really don't know what this could be related to, 开发者_如何学运维of course I can still use a constructor with 2 ore more arguments and the problem disappears but I'd like to know how is this possible.
PS: tstring
is just a macro I defined which resolves to wstring or string depending on the UNICODE settings.
Usually in such cases the problem is that the memory is being corrupted somewhere in your program. Since it's a DLL I suppose your OS is Windows. So you can use a free tool Application Verifier. Just set the Protect Heap Structures option on. It could help you to find the code which leads to the memory corruption.
精彩评论