As per an assignment, I've made a child class(PioneerWorld
) that inherits publicly from PioneerAm
which inherits from PioneerCarRadio
, which inherits from the base class CarRadio
.
I've then called a function that new the appropriate class object and returns the pointer to a PioneerCarRadio * pRadio
pointer.
The program then loops and the user can then choose to change the radio between PioneerCarRadio
, PioneerAm
, or PioneerWorld
. Choosing a different radio deletes the previous object in pRadio
and news the appropriate radio with the previously mentioned function.
The problem that occurs is that at any time after I choose to switch to PioneerWorld
and then exit the program, the Unhandled exception:
Unhandled exception at 0x7765806C (ntdll.dll) in A-06.exe: 0xC000000D: An invalid parameter was passed to a service or function. occurred
will occur after the return 0
for the main. Even if the program is exiting when not on the PioneerWorld radio.
It doesn't do this for any of the other radios only when PioneerWorld is chosen at any time during the program will this occur.
From what I can tell the problem resides in the PioneerWorld.h
file.
I have tried to add every single kind of exception catch in the try block (granted I don't believe I have a firm knowledge of how the try-catch exception handling works) but none have fixed the problem.
I have run it in debug mode and everything runs smoothly until:
printf("\nGoodbye!\n");
return 0;
} **<-- HERE**
I have tried to place the PioneerWorld higher up on the inheritance hierarchy (eg. rather than
CarRadio-> PioneerCarRadio-> PioneerAm-> PioneerWorld
I changed it to
CarRadio-> PioneerCarRadio-> PioneerWorld-> PioneerAm
And the same problem still occurs. (Exiting anytime after pRadio has been switched to PioneerWorld radio causes the error but the error does not occur if the radio was never switched to PioneerWorld)
I am honestly at a loss. ;_;
Main:
#include <stdio.h>
#include <string.h>
#include <new.h>
#include <exception>
#include <conio.h>
#include "PioneerWorld.h"
#include <iostream>
#pragma warning(disable :4996)
using namespace std;
//Prototype
PioneerCarRadio * createRadio(char *s);
int main(int argc, char * argv[])
{
//Variables
PioneerCarRadio * pRadio = NULL;
char *arg = argv[argc - 1];
char previousArg[8] = "";
char selection = 0;
//Loops the main process
do
{
//Checks if the previous argument is the same as the current, if not then create a new radio
if (strcmp(previousArg, arg) != 0)
{
//Creates the radio and reads any exceptions thrown
try
{
pRadio = createRadio(arg);
strcpy(previousArg, arg);
}
catch (char* e) //If argument is not present or invalid, exception is caught here
{
printf("\n EXCEPTION: Invalid switch modifier.\n");
return -1;
}
catch (bad_alloc& ba) //If no memory can be allocated in the instantiation of the radio, exception is caught here
{
printf("\n EXCEPTION: Cannot allocate memory.\n");
return -2;
}
catch (bad_exception& e) //Catch exceptions for unknown errors
{
printf("\n EXCEPTION: Unknown Excpetion Error.\n");
return -3;
}
catch (runtime_error& re)
{
printf("\n EXCEPTION: Runtime error\n");
return -4;
}
}
//Shows the currents settings for radio
pRadio->ShowCurrentSettings();
//Gets the user inputed selection
selection = pRadio->GetInput();
//Make the selection setting change (if appilicable)
pRadio->MakeSelection(selection);
//Redisplay the settings in case they had been adjusted
pRadio->ShowCurrentSettings();
//Checks what selection was made and changes the argument to the appropriate syntax
if (selection == 'c')
{
strcpy(arg, "-car");
}
else if (selection == 'a')
{
strcpy(arg, "-am");
}
else if (selection == 'w')
{
strcpy(arg, "-world");
}
//Checks if the previous argument is the same as the current, if not then delete the current object
if (strcmp(previousArg, arg) != 0 || selection == 'x')
{
//Deletes the current object
try
{
delete pRadio;
}
catch (bad_exception& e) //Catches any unexpected exceptions
{
printf("\n EXCEPTION: Unknown Exception\n");
return -5;
}
catch (runtime_error& re)
{
printf("\n EXCEPTION: Runtime error\n");
return -6;
}
}
} while (selection != 'x'); //Exits when user selects 'x'
printf("\nGoodbye!\n");
return 0;
}
PioneerCarRadio * createRadio(char *s)
{
//Variables
PioneerCarRadio * returnPtr = NULL;
//Check if the parameter is there and valid
if (s == NULL)
{
throw s; //throw the parameter
}
else if (strcmp(s, "-car") == 0)
{
//News the PioneerCarRadio and throws the pointer if NULL
if ((returnPtr = new PioneerCarRadio(false)) == NULL)
{
throw returnPtr;
}
else
{
return returnPtr;
}
}
else if (strcmp(s, "-am") == 0)
{
//News the PioneerAM and throws the pointer if NULL
if ((returnPtr = new PioneerAM(false)) == NULL)
{
throw returnPtr;
}
else
{
return returnPtr;
}
}
else if (strcmp(s, "-world") == 0)
{
//News the PioneerWorld and throws the pointer if NULL
if ((returnPtr = new PioneerWorld(false)) == NULL)
{
throw returnPtr;
}
else
{
return returnPtr;
}
}
else
{
throw s;
}
return NULL; //Default return
}
Problem Child (PioneerWorld.h
file):
#include <stdio.h>
#include"PioneerAM.h"
//Class
class PioneerWorld : public PioneerAM
{
private:
public:
PioneerWorld(bool power, char *name = (char *)"Pioneer XS440-WLRD") : PioneerAM(power, name)
{
ChangeCurrentStation(531);
}
virtual ~PioneerWorld()
{
printf("Destroying Pioneer XS440-WRLD Radio.\n");
}
virtual void ScanUp()
{
Freqs storedAM = GetStoredFreqs();
//if current_station is 1700, the current_station becomes 530
if (GetCurrentStation() >= 1602)
{
ChangeCurrentStation(531);
storedAM.AMFreq = GetCurrentStation();
}
else
{
ChangeCurrentStation(GetCurrentStation() + 9);
storedAM.AMFreq = GetCurrentStation();
}
}
virtual void ScanDown()
{
Freqs storedAM = GetStoredFreqs();
//if current_station is 1700, the current_station becomes 530
if (GetCurrentStation() <= 531)
{
ChangeCurrentStation(1602);
storedAM.AMFreq = GetCurrentStation();
}
else
{
ChangeCurrentStation(GetCurrentStation() - 9);
storedAM.AMFreq = GetCurrentStation();
}
}
};
The program should run until it is terminated without error and catch any exceptions, but instead if exiting the program at any time after radio was switched to PioneerWorld radio the program will execute normally until the main's return 0; at which point, on the next line (the '}') visual studios comes up with a error:
Unhandled exception at 0x7765806C (ntdll.dll) in A-06.exe: 0xC000000D: An invalid parameter was passed to a service or function. occurred
This is my first post so if there's anything wrong or you'd like more of the code to better understand what is the problem just ask.
Thanks for the help!
User contributions licensed under CC BY-SA 3.0