here is code
if(LOWORD(wParam)==IDC_B1)
{
TCHAR *text=new TCHAR[256];
// Set some text
delete[] text;
}
"B.S" <giob...@gmail.com> wrote in message
news:46aa2246-568e-4e97...@a21g2000yqc.googlegroups.com...
delete[] text;
}
FillOpenParams is returning plaithment of file. i tryed without delete
[] text and it worked fine but with it it crushes always.
Timo
--
www.TimoSoft-Software.de - Unicode controls for VB6
"Those who sacrifice freedom for safety deserve neither."
"Demokratie ist per Definition unsicher. Ihr Schutz entsteht aus der
Überzeugung, dass die demokratischen Kräfte überwiegen und sich – auf
demokratischem Wege – durchsetzen."
You are probably hoping that line copies text into the text array, but
it does not. It copies a pointer into the text pointer. So the
original text pointer is erased, which is why the later delete []
fails.
What is the type that is returned by FillOpenParams and why are you
casting it to TCHAR*? Nobody can show you how to fix it without that
missing information.
char *FillOpenParams( HWND hwnd, int ind)
{
OPENFILENAME open_params = {0};
char filter[128] = {0};
char file_name[512] = {0};
strcat(filter,"All File");
int index = strlen(filter) + 1;
filter[index++] = '*';
filter[index++] = '.';
filter[index++] = '*';
open_params.lStructSize = sizeof(OPENFILENAME);
open_params.hwndOwner = hwnd;
open_params.lpstrFilter = filter;
open_params.lpstrFile = file_name;
open_params.nMaxFile = 1024;
open_params.lpstrInitialDir = NULL;
open_params.lpstrFileTitle = NULL;
open_params.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST ;
if (ind==OPEN)
{
if(!GetOpenFileName(&open_params)) {return 0;}
return open_params.lpstrFile;
}
if (ind==SAVE)
{
if(!GetSaveFileName(&open_params)) {return 0;}
return open_params.lpstrFile;
}
else {MessageBox(hwnd,"unnown identifier",0,0);}
}
> char file_name[512] = {0};
> open_params.lpstrFile = file_name;
> return open_params.lpstrFile;
The memory occupied by 'file_name' is freed when the function is left,
so you are returning the address to a memory block that no longer will
be valid when it is accessed. Access violation I here you coming...
bool FillOpenParams(HWND hwnd, int ind, char *text)
{
.....
open_params.nMaxFile = MAX_PATH;
.....
if (ind==OPEN)
{
if(!GetOpenFileName(&open_params)) {return false;}
strcpy(text, open_params.lpstrFile); // copy to the buffer of the caller
return true;
}
// change other code likewise
........
}
// caller
if(LOWORD(wParam)==IDC_B1)
{
char *text=new char[MAX_PATH];
// Set some text
if(FillOpenParams(hwnd, ind, text)) DoSomething();
delete[] text;
}
> strcpy(text, open_params.lpstrFile); // copy to the buffer of the caller
This also is prone to buffer overflows. Strcpy copies until it finds a
null character. It doesn't take the buffer size into account. It's
better to use lstrcpyn here or even better: StringCchCopy.
> Friedel Jantzen schrieb:
>> bool FillOpenParams(HWND hwnd, int ind, char *text)
> This is prone to buffer overflows. The function doesn't know the size of
> 'text' and can only guess how many characters this buffer can take. The
> buffer size should be passed, too.
>
>> strcpy(text, open_params.lpstrFile); // copy to the buffer of the caller
> This also is prone to buffer overflows. Strcpy copies until it finds a
> null character. It doesn't take the buffer size into account. It's
> better to use lstrcpyn here or even better: StringCchCopy.
>
> Timo
You're right. StringCchCopy is recommended. And will work, if you input the
correct max length ;-)
If I know that the source string will fit into the dest. buffer, is it
necessary to do extra checking?
If the user can select only a single file, as the OP posted,
and
open_params.nMaxFile = MAX_PATH;
can the resulting string be longer than MAX_PATH?
Regards,
Friedel
> Well, such limitations may be lifted as time moves on and then you'll be
> glad to have written the code in a defensive way. ;)
I agree, that the defensive way is important to write robust software,
and admit using StringCchCopy very often. IMO especially important on user
input or reading from files, when I do not know the length of the strings.
> You're right in this specific case. But if you don't avoid strcpy in
> general, time will come when you make a failure and decide for strcpy
> when you really should use lstrcpyn. I prefer to always use at least
> lstrcpyn. It may be less performant, but it's also less likely I make a
> failure that may result in a buffer overflow.
I used lstrcpyn for a long time, but switched to StringCchCopy some time
ago.
> You really should pass FillOpenParams the buffer size. Or how do you
> make sure that you never call this function from another part of your
> code, with a buffer smaller than MAX_PATH chars?
In this case I am sure; for single selection file open dialogs I always
provide [MAX_PATH] sized buffers. For my purposes I have written some well
tested standard functions and C++ classes for open/save file dialogs.
However in my posting I tried to reuse the code of the OP as much as
possible (it could be improved...).
Regards,
Friedel