|
Hi
I have a function
void TakeData(myStruct &structRef)
{
CString str = TmpFileMgr->TmpFilePathName() ;
structRef.cstrFilePath = str ;
}
in a dll which takes Temporary folder path and assign it in a CString variables. This CString variable is member of structure object which is passed as a reference to TakeData(myStruct &structRef)function.
I am able to print temporary folder path in TakeData function and CString assigment works perfectly on Windows XP. But when I run this code on Windows 7 it crashes at the point where I am doing assigment of temp folder path in a CString variable. I have all admin rights on Windows 7 System , also I have put UAC to OFF.
Please provide any input in this regard. Thanks
modified 3-Dec-12 10:16am.
|
|
|
|
|
pandit84 wrote: But when I run this code on Windows 7 it crashes at the point where I am doing assigment of temp folder path in a CString variable. And yet this is the code you do not show. If TakeData() is not pages long, please post it.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
What is the definition of myStruct , and what is the value of structRef when the crash occurs?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Struct is defined as below. Before crash ,value of cstrFilePath is empty.
struct myStruct
{
int charsize ;
CString cstrFilePath;
myStruct() : charsize (0)
{
cstrFilePath.Empty();
}
} ;
|
|
|
|
|
pandit84 wrote: Before crash ,value of cstrFilePath is empty. Which is rather irrelevant. What I asked was, what is the value of structRef when the crash occurs?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
You have supplied very little to go on, so in addition to suggesting you provide more information I'll ask a question: are you allocating memory in one module and freeing it in another (a module is a dll or exe)?
Steve
|
|
|
|
|
I am sorry for adding incomplete info.
I have allocated memory to object of structure myStruct in one dll and passed this struct object as a reference to another dll. This crash happens only while assinging value to
structRef.cstrFilePath variable. Finally I was able to assign value inside CString variable using
wsccpy method. I was able to resolve this crash with following code , but I am sure this is not the correct way to do it.
wcscpy (structRef.cstrFilePath.GetBuffer (), str.GetBuffer ()) ;
I am still trying to resolve this issue in a correct way. I am using Windows 7 system and GCC Compiler. Thanks all for replying to my previous post.
|
|
|
|
|
Unless you configure things properly each module (dll or exe or other executable module) has it's own heap. In this environment you can't allocate memory in one module and free it another. Given that CString manages a buffer using an instance in multiple modules could result in this happening. Forget wcscpy, it's ugly and isn't a fix for the root problem, whatever it is. If it's heap corruption, which is my guess, it's going to blow up sooner or later and "fixing" one problem with an ugly hack will just result in it popping up somewhere else.
Steve
modified 5-Dec-12 11:14am.
|
|
|
|
|
Finally I have solved this issue.
The CString when used with GCC was Used as LinuxCString which is internally std::string. Capacity of std::string when calculated was every time 0. so when I tried to assign const char* to LinuxCString ( i.e. CString in MSVC ) it was crashing.
I have used std::string.reserve (200) method to specify the capacity first while initialization. This solves my issue.
I am still evaluating whether my fix is perfect or not.
Thanks all
|
|
|
|
|
The fix is far from perfect, you shouldn't need to call reserve. If I were you I'd be looking for the real problem.
Steve
|
|
|
|