Issue with crash at exit

Hey guys,

Not sure what is happening here, but all of a sudden I started having crash issues when I close my program. The thing is, it started happening after I added a destructer to a class I made. I'll be honest when it comes to classes and destructors I'm pretty new so I really don't even fully understand how those actually work, or when they need to be used. I just heard that classes should have destructors and that not having them could lead to memory leaks. So, even though everything was running good, I thought I should probably add a destructor to this class.
The first time I compiled and ran after adding the destructor, it crashed when exiting. After googling for a while I found a function "int _CrtDumpMemoryLeaks( void );". I read that I should use this right before the program exits and see if there is any leaks. Here is what I got:



First-chance exception at 0x0058DF58 (msvcp120d.dll) in SINDER.exe: 0xC0000005: Access violation reading location 0x00000036.
Unhandled exception at 0x0058DF58 (msvcp120d.dll) in SINDER.exe: 0xC0000005: Access violation reading location 0x00000036.

First-chance exception at 0x7710017E (ntdll.dll) in SINDER.exe: 0x00000000: The operation completed successfully.
The thread 0x4060 has exited with code -1073741510 (0xc000013a).
The thread 0x410c has exited with code -1073741510 (0xc000013a).
The thread 0x40a0 has exited with code -1073741510 (0xc000013a).
The thread 0x47cc has exited with code -1073741510 (0xc000013a).
The thread 0x3e4c has exited with code -1073741510 (0xc000013a).
The thread 0x3cac has exited with code -1073741510 (0xc000013a).
The thread 0x1e54 has exited with code -1073741510 (0xc000013a).
The thread 0x41cc has exited with code -1073741510 (0xc000013a).
The thread 0x3dc4 has exited with code -1073741510 (0xc000013a).
The thread 0x3cf8 has exited with code -1073741510 (0xc000013a).
The thread 0x7d4 has exited with code -1073741510 (0xc000013a).
The thread 0x3dbc has exited with code -1073741510 (0xc000013a).
The thread 0x4774 has exited with code -1073741510 (0xc000013a).
The thread 0x4044 has exited with code -1073741510 (0xc000013a).
The thread 0x4068 has exited with code -1073741510 (0xc000013a).
The program '[14576] SINDER.exe' has exited with code -1073741510 (0xc000013a).


Where it says "the thread 0xblah has exited with code -1073741510 (address?)
Is that the leak? If so how do I clean it up? and stop the program from crashing.

Also, I didn't post it, but there are other statements like this that say "code returned 0" before this is displayed. I'm guessing that is memory that has been cleared? Any help on this would be appriciated. If needed I will supply the source if it helps with debugging this issue.
it started happening after I added a destructer to a class I made.

Sometimes one can notice logic errors simply by looking at the code ...
If your implying that I probably didn't get rid of the destructor, therefor the problem persists. That is not the case. I did get rid of it once I started having the issue. I'm not that bad at troubleshooting.
Post the code that is causing the problem?
sent you a PM james
btw I just found out it only does it when in debug. as opposed to release.
MSVC release and debug builds?

Their binaries are quite different. For example the array element dereferencing.
* The debug version checks used indexes against out-of-range errors and will throw.
* The release is "lean and mean", as in silent, unchecked, undefined behaviour on errors.

In other words, the release version is likely be at least as erroneous as the debug,
but does not show it. That is worse than a crash that reveals a problem.

I did get rid of it once I started having the issue.

Did you revert to a version before adding the destructor, or did you manually undo changes?
Can you actually show us your code? There's no way we can tell what's wrong with it, if we can't see it.
Keskiverto, I manually removed it.

Mike its thousands of lines. I can't really post it. This class I made is of class player. I only use one instance of it. It encompasses all the values that would be associated with a player ie health, hunger, all player based stats.


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
//at the beginning of my main.cpp after includes and using, I just make one instance of player

Player player;

int main()

//Later on I may write something like...
sf::String healthVal = to_string(player.getHealth());
sf::Text healthValText = setText(healthValText, healthVal, thisFont, thatColor, xSize, yPosition);
//inside player.h



class Player
{
private:
       string m_name;
	char m_gender;
	int m_age;
	int m_level;
	int m_Xp;
	int m_search;
	string m_location;
	unsigned int m_health;
	int m_hunger;
	int m_thirst;
	int m_energy;
	string m_pack[10];
	int m_ammoPouch[8];
	string m_skills[12];
	string m_armor;
	string m_primary;
	string m_sideArm;
	string m_melee;
	string m_gadget;
	int m_wallet;	
	int m_focus;
	int m_agility;
	int m_strength;
	int m_toughness;
	int m_smarts;
	int m_charm;
	int m_armorScore;

public:
	Player();
        
       ~Player();  //<- this is all I added before the issue, then manually removed once I had the issue

Player::Player()
{
	m_health = 100;
	m_hunger = 100;
	m_thirst = 100;
	m_energy = 100;
	m_level = 1;
	m_Xp = 0;
	m_search = 10;
	m_armorScore = 3;
	m_agility = 10;
	m_strength = 10;
	m_focus = 10;
	m_toughness = 10;
	m_charm = 10;
	m_smarts = 10;
	m_location = "Home";
	for (int i = 0; i < 10; i++)
	{
		m_pack[i] = "Nothing";

		if (i <= 8)
		{
			m_ammoPouch[i] = 50;
		}
	}
	m_skills[0] = "Survival :";
	m_skills[1] = "First Aid :";
	m_skills[2] = "Hunting :";
	m_skills[3] = "Search :";
	m_skills[4] = "Pistals :";
	m_skills[5] = "Rifles :";
	m_skills[6] = "Melee :";
	m_skills[7] = "Stealth :";
	m_skills[8] = "Lockpicking :";
	m_skills[9] = "Toughness :";
	m_skills[10] = "Stiking :";
	m_skills[11] = "Explosives :";
	m_armor = "T-Shirt and Shorts";
	m_primary = "Nothing";
	m_sideArm = "Nothing";
	m_melee = "Fists";
	m_gadget = "Nothing";
	m_wallet = 20;
}


I know this is kind of messy but I dont really know what to show you guys to figure this out. : /
I dont know if this helps but when I get the crash promt "this.exe has stopped working", under the view problem details It reads...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Problem signature:
  Problem Event Name:	APPCRASH
  Application Name:	        Sinder.exe
  Application Version:	        0.0.0.0
  Application Timestamp:	55bc4343
  Fault Module Name:	MSVCP120D.dll
  Fault Module Version:	12.0.21005.1
  Fault Module Timestamp:	524f7ce9
  Exception Code:	        c0000005
  Exception Offset:	        0000df58
  OS Version:	                6.1.7601.2.1.0.768.3
  Locale ID:	                1033
  Additional Information 1:	0a9e
  Additional Information 2:	0a9e372d3b4ad19135b953a78882e789
  Additional Information 3:	0a9e
  Additional Information 4:	0a9e372d3b4ad19135b953a78882e789
Exception Code: c0000005 is an access violation so you accessing memory you shouldn't be.

1
2
3
4
5
6
7
8
9
10
11
int m_ammoPouch[8];

for (int i = 0; i < 10; i++)
{
	m_pack[i] = "Nothing";

	if (i <= 8)
	{
		m_ammoPouch[i] = 50;
	}
}


You are going out of bounds here which causes undefined behavior. m_ammoPouch only goes from 0-7 you shouldn't be using <= 8.

If that doesn't fix it check any other places you could be doing this.
James, You were already the man by simply sharing the same name as me. But now you are on another level my friend!
I changed the ammopouch[8] to [9], and bam...no more crash.
Damn, rookie mistake ha ha!

Question: why does this error occur when closing the program, as opposed to when the for loop executes?

Thanks again!
Last edited on
Glad that fixed it.

Question: why does this error occur when closing the program, as opposed to when the for loop executes?

Someone can probably give you a really technical answer but the simple answer is "because C++". In some other languages it would warn you earlier but C++ gives you the freedom to shoot yourself in the foot.

When you read/write to memory you are not meant to access anything can happen, including nothing at all (the worst case). It just happens the memory you overwrote wasn't serious enough to cause any major problems but upset the OS when it tried to clear all of the memory on the stack at the end of the program.
Hi,

Edit: Ninja'd - hopefully some of it is worthwhile :+)

If I could add my 2 cents worth, to help you out now. The very knowledgeable / expert keskiverto , James2250 , MikeyBoy haven't replied for awhile (different time-zone). I am just a dabbler, you should prefer to deal with these guys :+)

If one doesn't supply a destructor, then the compiler will implicitly create one for you. Presumably, it goes through and properly destroys each member in your object. So I guess this is why it works when you don't have a dtor.

When you do supply a dtor that does nothing, I imagine that MSVC is complaining about members of of your object not being destroyed properly.

I just heard that classes should have destructors and that not having them could lead to memory leaks.


That applies if you are allocating on the heap as opposed to on the stack. There are other situations too, like if you have virtual functions - you should provide a virtual dtor. If none of that applies to you, then you may well not need a destructor. Even if you do need one, you can force default behaviour with this:

~Player() = default; // C++11

Just some other stuff I can see about your code already, nothing to do with your problem, just mentioning them to help you out, or give a different insight:

Learn about constructor initialiser lists, and how they are better than the assignment you are doing at the moment. Also about default values. Here are some partial examples:



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
// initaliser list

Player::Player (
        // Base class parameters here
        std::string name = "Ginger";  // parameters in same order as class declaration, don't miss any of them
	char gender = 'F';    // put comments about valid range of values
 	int age = 20;           // could be unsigned short??
	 int level = 1;            // could be unsigned short??
	int Xp;                    // other default vlaues
	int search;
        // and so on ......
)

: // colon introduces initilaser list - values initialised before object is created
        // call base class constructors here, in order
        m_name(name);
	m_gender(gender);
	m_age(age);
	m_level(level);
	m_Xp(Xp);
	m_search(search);
         // and so on .....
{
    // do some validation checks here, set member IsValid to false, report error or throw exception
    // or more advanced do a function try block in initialiser list as well
}  // end constructor body
  


Also, One can use brace initialisation for arrays, as in :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   // compiler will count how items there are in array
std::string m_skills[]   = { "Survival :" , //Spread things out 1 per line then you can comment them if necessary
                                      "First Aid :" ,
	                              "Hunting :" ,
	                              "Search :" ,
	                              "Pistals :" ,
	                              "Rifles :" ,
	                              "Melee :" ,
	                              "Stealth :" ,
	                              "Lockpicking :" ,
	                              "Toughness :" ,
	                              "Stiking :" ,
	                              "Explosives :"
                                   };


Why is this array a member of the Player class? Could it be a struct that has other variables associated with each array item? For example a bool to say whether Player owns a thing or not.

Good luck !! :+D
Last edited on
thanks, TheIdeasMan! I will take your advice on those things forsure!
No worries pleased to help a little :+D
Extra bit:
Your player is a global variable. When are the ctor and dtor of a global variable called? Before and after main()? At least Herb Sutter has written about object lifetime. Guru of the Week series.

Local variables with well-defined lifetime are easier to debug too.
@keskiverto

Good Work !

++MajorThingsNoticed;

I wonder if we could convince the OP to post all the code on a dropbox somewhere - I am sure you guys could point out all kinds of things that might be improved.
If you guys want I could share it through dropbox. I could use a good programmers eye. I have to leave for work so It might be later tonight that I could do it.
Topic archived. No new replies allowed.