|
Hi there,
I'm new to C++ and i've got a basic project i've made up, just to play around with templates. It consists of the following classes:
- enumerable (used much like IEnumerable in .NEt Framework code)
- list<Type> [inherits from enumerable] (provides basic add & remove alongside initial sizes)
- keyValuePair<KeyType, ValueType> (is a simple struct with 2 properties - key and value...
- dictionary<KeyType, ValueType> [inherits from list] (is a list, which uses keyValuePair
No error appears to occur until the end of my program exection. At this point the program simply hangs while executing my final instruction:
delete [] pDict;
My program currently does something no more complex than adding a few initial items to a dictionary, retrieving a list object filled with keys and then cleaning up... So I pause the debugger, it warns me:
The process appears to be deadlocked (or is not running any user-mode code). All threads have been stopped.
and it then points me to:
dbgrpt.c (line 360 - 363):
nCode = __crtMessageBox(szOutMessage,
_T("Microsoft Visual C++ Debug Library"),
MB_TASKMODAL|MB_ICONHAND|MB_ABORTRETRYIGNORE|MB_SETFOREGROUND);
and points out that the line containing _T("Mi... is where the error occured. As far as i can understand, this portion of the "dbgprt.c" file displays an assertion failed error message. In case that helps any:
szOutMessage = "Debug Assertion Failed!
Program: ...iversity for the West of Scotland\Samples\Debug\Templates.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cppLine: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)";
I then proceded to search MSDN documentation and found nothing specific to help me... Unfortunately searches of this forum didn't help me get any further so here is my question:
Where should I look? and what should I be looking for as possible sources of the error?
I've checked for un-initialised variables, memory leaks, and ensured (i think) all functions that return a value have a variable to be copied into so as to clean the stack... and so far all seems fine.
Thankyou for reading.
|
|
|
|
|
First question: Was pDict allocated as an array, i.e.
pDict = new pDictType[size]; You are using the array delete syntax (delete []pDict ). If pDict is a simple pointer, then using the array delete will Do Bad Things.
Other than that, the general things I can suggest are to step through the code and ensure that you're not walking over something in the stack. The symptom you are describing (all threads exited) sounds like a stack overwrite.
|
|
|
|
|
Thanks for the resonse (and sorry about the delay) Yes, pDict was initialised as an array.
would you know of any areas or topics I might want to look into to refrence, or is it purely experience and luck :p
|
|
|
|
|
If the program is simple (your description made it sound like that was the case), single-stepping through it and verifying that each statement does exactly what you think it does is a valid approach. Look for side effects, possible buffer overruns, and things like that.
Barring that, you might post some of the code here, and let us have a look, if it's not horribly lengthy.
The STL container classes can occasionally be a double-edged sword. They are very capable, but I've seen folks do some foolhardy things with them (think void pointers, up and down casts, and that sort of thing).
|
|
|
|
|
I've just re-validated all of my code and found that your initial conclusion was correct - I intended to use a dictionary object, not an array called dictionary causing me to use the wrong delete statement...
Thankyou very much for your help - it has been much appreciated.
|
|
|
|
|
|
Wow - this was a freakin awesome feel-good thread
|
|
|
|
|
Yes it was; this is the way the programming forums are supposed to work.
Someone asks a reasonable question, showing that they did the work but are having trouble.
They follow the suggestions or respond with more details if asked.
They say "thank you" (that's a big one for me).
I've avoided the C++ forum in recent months, partly due to lack of time and partly because it had become a swamp of "do my homework for me" and "plz urntz send codez now!" posts.
|
|
|
|
|
Hi
I need to use a flexgrid in which i will be able to enter only Hexadecimal characters
from 0 - 9 and A - F (Capital letters only),which proprty should i use here in this case. Also i have to resize the column by clicking in the border of two columns heading just as we do in windows.
Please provide the comments.
modified on Saturday, February 21, 2009 9:46 AM
|
|
|
|
|
Hi,
I have developed program in which I am creating window.I want to maximize window size before its creation.Can anyone suggest me.
Rekha.
|
|
|
|
|
Override the PreCreateWindow method in the main window class.
Add the WS_MAXIMIZE style to the style member of the CREATESTRUCT parameter like so.
cs.style |= WS_MAXIMIZE
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Hi,
I have overriden PreCreateWindow like this.But I am unable to create maximized window.
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
cs.style &= ~FWS_ADDTOTITLE;
cs.style |= WS_MAXIMIZE;
return TRUE;
}
|
|
|
|
|
If you want to maximize the window call ShowWindow() with SW_MAXIMIZE . Take a look at the docs here.
Regards,
--Perspx
|
|
|
|
|
Hi,
I used ShowWindow(SW_MAXIMIZE) in PreCreateWindow function like this.But I
am unable to create maximized window.
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
f( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
cs.style &= ~FWS_ADDTOTITLE;
ShowWindow(SW_MAXIMIZE);
return TRUE;
}
|
|
|
|
|
Hi,
I am able to create maximized window.I added ShowWindow(SW_MAXIMIZE) in OnCreate function.Thanks for your reply
|
|
|
|
|
Hello,
How can I get the status of a service (Running, Stopped, etc) in
a remote machine. I've the domain (of the remote machine) username
and password.
I need to get the status programmatically without using any tool.
FYI : I'm using VC 6.0
Thanks in advance
|
|
|
|
|
You can give the remote machine name in the first parameter of OpenSCManager function.
Then use the QueryServiceStatusEx function to get the status of the service.
To impersonate another user you can use LogonUser and ImpersonateLoggedOnUser APIs.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Thanks for the info. I'll try those.
|
|
|
|
|
Using Following code Device Enumerator is Not Successful Created and Showing Message of "Error Creating Device Enumerator"
HRESULT hr;
ICreateDevEnum *pSysDevEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,IID_ICreateDevEnum, (void **)&pSysDevEnum);
if (SUCCEEDED(hr))
{
MessageBox("Succeded");
}
if (hr != S_OK)
{
MessageBox(TEXT("Error Creating Device Enumerator"));
return ;
}
Plz Suggest some Solution To Resolve this Problem
|
|
|
|
|
Using
HRESULT hr = CoInitialize(NULL);
Statement before Creating Enumerator Initialized Com library and Solved Problem.........
|
|
|
|
|
Following to my question yesterday, is there anything that can be done to clean up the c code below which works fine but looks ugly especially the strcat's? The program parses a file containing a list of files and then runs a command on each. I can do a similar thing in perl like this a couple of lines but in C its looks ugly and I think its because I am a newbie...any comments will be appreciated....
thanks in advance for everyones help
In perl:
#!/usr/bin/perl
while(<>){
chomp;
$cmd="qsub -P 9999 -wd -e $_\.error -b y /usr/local/projects/bin/program -i $_ -type p -terms -lookup -o $_\.qsub";
print "$cmd\n";
}
in C:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
char * cwd = getcwd(0,0);
char pro[10000];
char str[1000];
char* nl;
if (argc < 3) {
printf("\n\nPlease supply a file containing a list of single file names follow by project code\n");
exit(5);
}
FILE *f;
f = fopen(argv[1], "r");
if(!f) {
printf("Couldn't open file.txtn");
return 1;
}
while(fgets(str, 1000, f)) {
nl = strrchr(str, '\r');
if (nl) *nl = '\0';
nl = strrchr(str, '\n');
if (nl) *nl = '\0';
strcpy(pro, "qsub -P ");
strcat(pro, argv[2]);
strcat(pro, " -wd -e ");
strcat(pro, cwd);
strcat(pro, "/");
strcat(pro, str);
strcat(pro, ".error -b y /usr/local/projects/bin/program -i ");
strcat(pro, cwd);
strcat(pro, "/");
strcat(pro, str);
strcat(pro, " -type p -terms -lookup -o ");
strcat(pro, str);
strcat(pro, ".qsub");
printf (pro);
system(pro);
}
}
|
|
|
|
|
Hi,
why don't you use sprintf() to compute the entire string?
it works similar to printf() but outputs to a char buffer.
FYI: More modern languages support string objects and do simple concatenation, much like ancient Basic: string c = a + b;
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
You should really use sprintf() instead of strcpy and strcat, it will look something like this:
sprintf(pro, "%s%s%s%s%c%s%s%s%c%s%s%s%s",
"qsub -P ",
argv[2],
" -wd -e ",
cwd, '/',
str,
".error -b y /usr/local/projects/bin/program -i ",
cwd,
'/',
str,
" -type p -terms -lookup -o ",
str,
".qsub");
after this call pro[] buffer contains formatted string
And few remarks about your code safety:
be careful with creating huge local buffers on the stuck - pro[] buffer in this case. It may happen that you get stuck overflow (application crush) It is better to create such buffers dynamically then you can check if allocation succeeded or just create static buffer
use sizeof(str) in fgets(str, sizeof(str), f) to avoid problems with writing outsie str[] memory area when str[] buffer size will be changed to smaller one
|
|
|
|
|
Thank you so much grzkas!
|
|
|
|
|
One little (possible) improvement to the original answer - the literal strings can be embedded in the sprintf format string, as shown below. It's a bit more like variable interpolation in strings, a la Perl.
sprintf(pro, "qsub -P %s -wd -e %s/%s.error -b y /usr/local/projects/bin/program -i %s/%s -type p -terms -lookup -o %s.qsub",
argv[2],
cwd,
str,
cwd,
str,
str);
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|