Click here to Skip to main content
       

C / C++ / MFC

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCString assignment crashes on Windows 7 [modified]memberpandit843-Dec-12 3:57 
Hi
I have a function
void TakeData(myStruct &structRef)
{
  CString str = TmpFileMgr->TmpFilePathName() ; //Returns file path 
  structRef.cstrFilePath = str ;                //Crashes at this point on Windows 7
}

 
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.

SuggestionRe: CString assignment crashes on Windows 7memberDavidCrow3-Dec-12 4:08 
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


AnswerRe: CString assignment crashes on Windows 7mvpCPallini3-Dec-12 4:15 
I suppose this is worth reading: "Using and Writing DLLs with MFC"[^].
Veni, vidi, vici.

AnswerRe: CString assignment crashes on Windows 7mvpRichard MacCutchan3-Dec-12 5:02 
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.

GeneralRe: CString assignment crashes on Windows 7memberpandit843-Dec-12 5:41 
Struct is defined as below. Before crash ,value of cstrFilePath is empty.
struct myStruct
{
	int charsize ;	 
	CString	cstrFilePath;
	myStruct() : charsize (0)
	{
	    cstrFilePath.Empty();
	}
} ;

GeneralRe: CString assignment crashes on Windows 7mvpRichard MacCutchan3-Dec-12 6:22 
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.

AnswerRe: CString assignment crashes on Windows 7memberStephen Hewitt4-Dec-12 6:34 
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

GeneralRe: CString assignment crashes on Windows 7memberpandit844-Dec-12 19:44 
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.
GeneralRe: CString assignment crashes on Windows 7 [modified]memberStephen Hewitt5-Dec-12 3:49 
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.

AnswerRe: CString assignment crashes on Windows 7memberpandit8418-Dec-12 7:48 
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
GeneralRe: CString assignment crashes on Windows 7memberStephen Hewitt18-Dec-12 17:44 
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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 18 Jun 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid