|
I have the following piece of code.
char* strFile = new char[50] ;<br />
CString str( "Hello\nCoders" ) ;<br />
strFile = str.GetBuffer( str.GetLength() );<br />
std::ofstream myfile;<br />
myfile.open ("d:\\example.txt");<br />
myfile << strFile ;<br />
myfile.close() ;<br />
delete[] strFile ;
The program throws a Debug assertion error at delete[] strFile ; . The function throwing the Exception is _CrtIsValidHeapPointer(pUserData) .
I was of the impression that creation with new must be followed by cleanup with delete . Seems like I am wrong or I did something wrong in the above code. Can anyone explain?
---
Hakuna-Matada
It means no worries for the rest of your days...
It's our problem free, Philosophy
<marquee behavior="alternate" scrollamount="5" scrolldelay="50">
|
|
|
|
|
Hakuna-Matada wrote: char* strFile = new char[50] ;
strFile = str.GetBuffer( str.GetLength() );
when the scope of the variable ends, the destructor of the CString variable will be called. as per the implemantation of CString if you call GeBuffer, you should call ReleaseBuffer before calling any other functoins in CString else it will throw a debug asertion. Here the destructor itself a "silent" function. Hope you clear
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
In addition to what sarath said
char* strFile = new char[50] ;
strFile = str.GetBuffer( str.GetLength() ); causes a memory leak.
char* strFile = 0;
strFile = str.GetBuffer( str.GetLength() );
this is enough. And dont call delete.
nave
|
|
|
|
|
In addition to Naveen said,
Use GetBuffer only if you are doing some modifications on the CString.
operator LCTSTR (const char*) has been overloaded in CString for reading purpose.
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
Hi. Thanks for your reply. Well now I have..
char* strFile = new char[50] ;<br />
CString str( "Hello\nCoders" ) ;<br />
strFile = str.GetBuffer( str.GetLength() );<br />
str.ReleaseBuffer() ;<br />
std::ofstream myfile;<br />
myfile.open ("d:\\example.txt");<br />
myfile << strFile ;<br />
myfile.close() ;<br />
delete[] strFile ;
but it still doesn't work. As you may have noticed, I used the ReleaseBuffer as you had suggested but to no avail. Am I doing something wrong?
---
Hakuna-Matada
It means no worries for the rest of your days...
It's our problem free, Philosophy
<marquee behavior="alternate" scrollamount="5" scrolldelay="50">
|
|
|
|
|
why you are using the same buffy code?
This will work fine...
CString str( "Hello\nCoders" ) ;
std::ofstream myfile;
myfile.open ("d:\\example.txt");
myfile << (LPCTSTR)str;
myfile.close() ;
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
Last modified: Wednesday, August 02, 2006 1:15:50 AM --
|
|
|
|
|
Thanks for your time. Now I understand.
---
Hakuna-Matada
It means no worries for the rest of your days...
It's our problem free, Philosophy
<marquee behavior="alternate" scrollamount="5" scrolldelay="50">
|
|
|
|
|
Make sure you call ReleaseBuffer after a GetBuffer or GetBufferSetLength.
This works:
<br />
CString str( "Hello\nCoders" ) ;<br />
std::ofstream myfile;<br />
myfile.open ("c:\\example.txt");<br />
myfile << str.GetBuffer( str.GetLength() ) ;<br />
str.ReleaseBuffer();<br />
myfile.close() ;<br />
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
Steve Echols wrote: myfile << str.GetBuffer( str.GetLength() ) ;
In this code, it's not doing any editing. So that we can simply use the string object like this
myfile << (LPCTSTR) str;
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
You are correct sir. I think I was just trying to stress the ReleaseBuffer call if you call GetBuffer.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
|
You are first allocated a buffer on the heap, of size 50. You store the pointer in strFile. Then you do a CString::GetBuffer on a CString object, and overwrite the value in strFile with this new value, thus creating a storage leak.
Also, there is no need for the parameter to GetBuffer.
When you call delete, which frees the CString buffer out
from under the CString, then the CString destructor sees a completely screwed-up heap.
LPTSTR strFile = str.GetBuffer();
std::ofstream myfile;
myfile.open ("d:\\example.txt");
myfile << strFile ;
myfile.close() ;
Amar
|
|
|
|
|
You're misusing the return value of GetBuffer() . GetBuffer() returns a pointer to the buffer that the CString manages, you must not delete that pointer.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
Hi
i am developing a Dialog based application in VC++ MFC, i need to provide multi-language support to that application. can any one provide me some solution to that.
Thanks in advance for any help.
Gopinath MV
|
|
|
|
|
gloriousgopi wrote: multi-language support to that application
You can UNICODE
Do your Duty and Don't expect the Result
|
|
|
|
|
Thanks for u suggestion.
Can you plz explain it some more. So that i can able to work on it
Gopinath MV
|
|
|
|
|
|
My problem is to provide multilanguage support for my application.
i,e i have to show the Dialog based application in English when the user clicks english from the menu, or in French when he clicks French from the menu.
when the user changes the language the application should change its whole display according to user choice
Gopinath MV
|
|
|
|
|
|
Thanks prasad_som.
The link is very helpful.
Gopinath MV
|
|
|
|
|
in dialog based application by clicking a button if i want to open a another window how it will b possible???is there any function for that which can b used there..
thanks
|
|
|
|
|
You can use
Create an object to the class and use 'DoModal()'
or
Create an dialog pointer and use ShowWindow(SW_SHOW)
DoModal() will disallow any actions in parent dialog untill the new dialog is closed
Do your Duty and Don't expect the Result
|
|
|
|
|
Is the window (dialog or window) part of your application? or and external application
For the dialog
You can call DoModal function of the dialog object
For SDI, MDI or or other type of windows, You can call ShowWindow function to show the dialog
It should be handled inside the message handler of the button
If an external application, you can Make use of CreateProcess API.
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
if its another dialog then call it using DoModal() function of the instance of the dialog.
Eg MyDialog dlg;
dlg.DoModal();
if its a window then call the ShowWindow() method
eg ShowWindow(this->m_hWnd,SW_SHOW);
regards
Gopinath.
Gopinath MV
|
|
|
|
|
can u help me when i use this function i got the error
"
C:\myproject\newartz\newartzDlg.cpp(204) : error C2660: 'ShowWindow' : function does not take 2 parameters"
Sorry to bother u pls help me out
eg ShowWindow(this->m_hWnd,SW_SHOW);
|
|
|
|