Run Time Exception in ctr0dat.c

I have a procedure in my code, that whenever I have defined a class object or a structure object in it, and tried to assign values with set methods, throws a run time exception in crt0dat.c (when using set methods). If this definitions are in some of the other procedures, they don't have run time errors.

The error appears in crt0dat.c in this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/***
* _unlockexit - Release exit code lock
*
*Purpose:
*       [See _lockexit() description above.]
*
*       This routine is called by _cexit(), _c_exit(), and onexit()/atexit().
*       The exit() and _exit() routines never unlock the exit code path since
*       they are terminating the process.
*
*Entry:
*       Exit code path is unlocked.
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

void __cdecl _unlockexit (
        void
        )
{
        _munlock(_EXIT_LOCK1);
}


I have tried everything to fix this, and nothing worked. I am in despair!!!
I think it'd be more useful for you to show us your code that is actually causing the problem.
I will try, cause I have so MUCH code. This is the function where the error appears (I have commented before the line of code where it actually fails):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
static std::list<Sense> WNOverview(char *searchstr, int pos)
{
    SynsetPtr cursyn;
    IndexPtr idx = NULL;
    char *cpstring = searchstr, *bufstart;
    int sense, i, offsetcnt;
    int svdflag, skipit;
    unsigned long offsets[MAXSENSE];
	std::list<Sense> synsets;
    cpstring = searchstr;
    bufstart = searchbuffer;
    for (i = 0; i < MAXSENSE; i++)
	offsets[i] = 0;
    offsetcnt = 0;
	int TaggedNo=0;
    while ((idx = getindex(cpstring, pos)) != NULL) {

	cpstring = NULL;	/* clear for next call to getindex() */
	wnresults.SenseCount[wnresults.numforms++] = idx->off_cnt;
	wnresults.OutSenseCount[wnresults.numforms] = 0;

	printbuffer("          \n");

	/* Print synset for each sense.  If requested, precede
	   synset with synset offset and/or lexical file information.*/
	
	std::string sense_str;
	std::string eg_use;
	
		
	for (sense = 0; sense < idx->off_cnt; sense++) {

	    for (i = 0, skipit = 0; i < offsetcnt && !skipit; i++)
		if (offsets[i] == idx->offset[sense])
		    skipit = 1;

	    if (!skipit) {
		offsets[offsetcnt++] = idx->offset[sense];
		cursyn = read_synset(pos, idx->offset[sense], idx->wd);

		
		sense_str="";
		eg_use="";

		sense_str=cursyn->defn;
		
		char *char_array;
		char_array=(char *)malloc(sizeof(sense_str.c_str()+1));
		strcpy(char_array, sense_str.c_str());
		
		char *sense_char;
		char *egUse_char;

		if(cursyn->defn)
		{
			sense_char=strtok(char_array,"\"");
			egUse_char=strtok(NULL,"\"");
			sense_str=sense_char;
			if(egUse_char!=NULL)
			eg_use=egUse_char;
			else eg_use="";
		}
		else
		{
			strcpy(sense_char,"");
			strcpy(egUse_char,"");
			sense_str=sense_char;
			eg_use=egUse_char;
		}
		
		
		

		Sense word_sense;
// THIS IS WHERE EXCEPTION OCCURS, and if this code is placed before while or in another procedure, it works 
		word_sense.SetNodeLabel(sense_str);
		word_sense.SetExampleUseString(eg_use);
		
		
		//change: insert a synset in the list
		synsets.push_back(word_sense);

		free_synset(cursyn);
	    }
	}

	wnresults.numforms++;
	free_index(idx);
    }
	return synsets;
}
Unfortunately, it looks like you're going to have to debug this code to try and find your fault.

Is there really such a need to mix and match C and C++ coding methodologies so badly? If you're going to be using C++ then I'd suggest using it exclusively where possible. Avoid the use of C constructs like malloc() and strcpy().
Well yes, I have to mix C and C++ since I am using a ready interface for the database I'm using. The interface is written in C and I have to use classes (C++) to implement my code on top of the interface. Do you have any idea why this line of code would throw a memory exception in crt0dat.c??? I have been trying for too long to fix it, I am becoming very desperate.
My only guess would be somewhere you've run over-bounds (buffer overflow) or you've writing to memory that hasn't been allocated. While this may not always cause an immediate crash it'll often bring up unexpected behavior at other locations in your application.

Because these things are hard to detect, I do suggest looking at the call-stack after your program has crashed and then working backwards to ensure all memory recently accessed was done so within bounds.
How can I check the call stack and work backwards? I was checking the memory with a profiler, and there was enough space...

I am working in Visual Studio 2008..

Please let me know how I can run these checks.

Thanks!
When it crashes. Visual Studio will give you a stack trace. This is a good place to start. Google "Debugging with Visual Studio" etc.
Topic archived. No new replies allowed.