Some guidance with exit code -1073741819 (0xC0000005)

Ok, I've been struggling a lot with what I think is a minor tweak somewhere.

When using debug mode everything is working as intended with the code that I'm done with but when using release I get error: "Process finished with exit code -1073741819 (0xC0000005)"

This is happening when using switch case 1 and trying to read data from a .txt-file. In debug mode it loads fine but in not when compiling normally.

Assignment is creating a jukebox which you can add albums to.

jukebox.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "constants.h"
#include "jukebox.h"
#include <iomanip>
#include <fstream>

Jukebox::Jukebox()

{
    file.setTitle("*** FILE ***");
    addAnAlbum.setTitle("*** ADD AN ALUM ***");
    deleteAnAlbum.setTitle("*** DELETE AN ALBUM***");
    print.setTitle("*** PRINT AN ALBUM ***");
}



void Jukebox::run()
{
    Menu menu;


    menu.addItem("1. File...", true);
    menu.addItem("2. Add an album", false);
    menu.addItem("3. Delete album", false);
    menu.addItem("4. Print...", false);
    menu.addItem("5. Exit program", true);


    bool again = true;

    do
    {


        /// Prints the menu added above.
        menu.printMenuItems();


        switch (menu.getMenuChoice()) {
            case 1:
                subMenuFile();
                break;

            case 2:
                addAlbum();
                break;

            case 3:
                ///blabla
                break;

            case 4:
                printAlbum();
                break;

            case 5:
                again = false;
                break;
        }
    } while (again);
}



void Jukebox::subMenuFile()
{
    file.addItem("1. Open", true);
    file.addItem("2. Save", false);
    file.addItem("3. Back to main menu", true);


    bool again = true;

    do
    {
        file.printMenuItems();

        switch (file.getMenuChoice())
        {
            case 1:
                loadfile();
                break;

            case 2:
                ///Code to come
                break;

            case 3:
                again = false;
                break;
        }


    } while (again);
}


void Jukebox::loadfile()
{
    std::ifstream inFile("../../_Resources/" + jukebox + ".txt", std::ios::in);
    Album tmpAlbum;

    if(inFile.is_open())
    {
        while(inFile >> tmpAlbum)
        {
            listOfAlbums.push_back(tmpAlbum);
        }

        std::cout << "File was loaded successfully!" << std::endl;

    } else
    {
        std::cout << "File could not be loaded!" << std::endl;
    }


    inFile.close();
}



jukebox.h
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
#include <string>
#include <iomanip>
#include <iostream>
#include <vector>
#include <algorithm>
#include "album.h"
#include "menu.h"

class Jukebox
{
private:

    std::vector<Album> listOfAlbums;
    Menu file;
    Menu addAnAlbum;
    Menu deleteAnAlbum;
    Menu print;
    void addAlbum();
    void subMenuFile();
    void deleteAlbum();
    void printAlbum();
    void loadfile();

    void Push(Album& album);


public:
    Jukebox();
    void run();
};


Can anyone notice anything obvious? Am I not initializing the vector? Is that really needed? THIS IS EATING ME UP
Am I not initializing the vector?
There is no need for it, the constructor will do it for you.
I can't see any obvious problems in your code.
while(inFile >> tmpAlbum) this could be a place where things could go wrong.
Can you show this code?
I think the problem code is elsewhere (in code you haven't posted.)

The vector member will be initialised (by its default constructor) as part of the construction of the Jukebox class. So there's nothing you need the change about that.

0xC0000005 = Access Violation, so somewhere code is trying to access inaccessible memory. Have you got a stack trace for the crash?

The debug build can behave differently for a number of reasons. One of them is that the debug memory allocation routines will fill newly allocated memory with a file pattern, whereas the normal allocation routines don't bother. They will be left with what ever random values were found there. So it's good practice to initialize newly allocated memory/variables.

Andy
Last edited on
So, when reading from file it is supposed to read like this:

Album name1
Number of songs in album 1
Title|Artist|Duration
Title|Artist|Duration
Title|Artist|Duration
Album name 2
Number of songs in album 2
Title|Artist|Duration
Title|Artist|Duration
etc etc.


Overloaded >> is done in three classes:

song, songTime and album:

>> for song:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::istream &operator>>(std::istream &is, Song &song)
{
    std::string tempTitle, tempArtist, duration;
    songTime songtime;

    getline(is,tempTitle, DELIM);
    song.setTitle(tempTitle);
    getline(is, tempArtist, DELIM);
    song.setArtist(tempArtist);
    getline(is, duration);

    if (!duration.empty() && duration.find_first_not_of("0123456789") == std::string::npos)
    {
        int songDURATION = std::stoi(duration);
        songtime.setSeconds(songDURATION);
        song.setSongDuration(songtime);
    }

    return is;
}


>> for songTime:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::istream &operator>>(std::istream &is, songTime& songtime)
{
    int tempHours, tempMinutes, tempSeconds;

    is >> tempHours;
    is >> tempMinutes;
    is >> tempSeconds;

    songtime.setHours(tempHours);
    songtime.setMinutes(tempMinutes);
    songtime.setHours(tempHours);

    return is;
}


>> for album:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::istream &operator>>(std::istream &is, Album &album)
{
    Song song;
    std::string tempAlbumName, tempTitle, tempArtist, tempSize;
    getline(is,tempAlbumName);
    album.setAlbumName(tempAlbumName);
    getline(is, tempSize);
    
    if (!tempSize.empty() && tempSize.find_first_not_of("0123456789") == std::string::npos)
    {
        int albumSize = std::stoi(tempSize);
        album.setAlbumSize(albumSize);

        for (size_t i = 0; i < albumSize; i++)
        {
            is >> song;
            album.addSong(song);
        }
    }
    
    return is;
}


What makes it work in debug mode? It's confusing me

EDIT:

I have a const char DELIM = '|'; in constants.h

EDIT 2:

When running it in repl.it I instead get error free(): invalid pointer.
If that makes stuff any clearer for you?
Last edited on
What makes it work in debug mode? It's confusing me
Luck. Plain simple luck. It happens all the time. The code contains a bug, but sometimes if works anyway. Most programs work by accident, not by design.

One problem with memory access problems is that the bug can be in one place while the symptom shows up somewhere completely different. For this reason, you need to post your entire program for us to help.
I think the problem code is elsewhere (in code you haven't posted.)

0xC0000005 = Access Violation, so somewhere code is trying to access inaccessible memory. Have you got a stack trace for the crash?

Very plausible!

I don't know how to get a stack trace from CLion, I can see if I can figure something out!
Ok, I'm just dumb. I had a destructor that was just deleting the song vector everytime program ran.

Thanks for the help nodding me to the right direction, I would have just continued watching the two functions that were invloved otherwise!

I'm sure I'll run into some more problems along the way, I WILL BE BACK!
Topic archived. No new replies allowed.