2

Background: So I've created an application that is basically a large preference dialog where the user can configure a number of pages, each with a bunch of different settings. These settings are in the form of dropdowns and text boxes. I want to store all of the variables into one massive "Parameters.h" file so that I can access them from anywhere in my application. Each sub-page has it's own source and header file.

I'm having trouble with the pointers though. I'm not sure how to reference the Parameters class. Basically, my application has two main components: a main dialog and a bunch of sub, child pages. The main dialog is where the sub-pages are shown and hidden, based on what page the user chooses in a listbox on the left of the main dialog.

I'm just working with one sub-page right now, and have the following, but when I debug, I'm getting <BadPtr> all over the place. I have greatly simplified the code, but it should be enough to figure out what I'm doing wrong.

Question: So how do I point to this Parameters class in each sub-dialog so that I can store and use all of these variables?


SAPrefsDialog.cpp: Main dialog that houses the sub-pages

BOOL CSAPrefsDialog::OnInitDialog() 
{
    CDialog::OnInitDialog();
    FSC_Main fscMain;
    fscMain.SetParametersPointer(&m_pParams);
    // [ ... ]
}

SAPrefsDialog.h: Main dialog header file

#include "Parameters.h"

public:
    CSAPrefsDialog(CWnd* pParent = NULL);   // standard constructor
   ~CSAPrefsDialog();

    Parameters m_pParams;

FSC_Main.h: Sub-page header file

#include "Parameters.h"

class FSC_Main : public CSAPrefsSubDlg
{
// Construction
public:
    FSC_Main(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
    //{{AFX_DATA(FSC_Main)
    enum { IDD = IDD_FS_CONFIG_MAIN };
    //}}AFX_DATA

public:

    void SetParametersPointer(Parameters* pParameters)
        { m_Params = pParameters; }

private:
    Parameters *m_Params;
};

Parameters.h

#include "stdafx.h"
#include "prefs.h"

#pragma once

class Parameters
{
public:
        Parameters();   // standard constructor
public:
    ~Parameters(void);

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

public:

//*****************************************************************************
//
// FSC_Main.cpp Variables
//
//*****************************************************************************

    CString m_strVehiclesMainNumVehicles;
    CString m_strVehiclesMainMaxSensorCount;
    CString m_strVehiclesMainTaskProcessingInterval;
    CString m_strVehiclesMain
    // [ ... ]

Parameters.cpp

#include "stdafx.h"
#include "prefs.h"
#include "pages.h"
#include "Parameters.h"

//*****************************************************************************
//
// Parameters::Parameters
//
//*****************************************************************************

Parameters::Parameters():
      m_strVehiclesMainNumVehicles("")
    , m_strVehiclesMainMaxSensorCount("")
    , m_strVehiclesMainTaskProcessingInterval("")
    // [ ... ]
{
}
Jon
  • 3,154
  • 13
  • 53
  • 96
  • Sorry, I wasn't explicit. Added. – Jon Jul 21 '11 at 13:45
  • learn how to use the debugger. you need to halt execution when the bad pointer is dereferenced – marinara Jul 21 '11 at 13:57
  • I'm using Visual Studio and have been adding breakpoints to see where things go awry, but then it won't let me step all the way through execution...not because of an error, just because it tries to go too deep into the execution path. – Jon Jul 21 '11 at 14:00
  • find the file that is echoing "bad ptr" (this may take time) and put a breakpoint there. once you have that done, click on the call stack to see where it's being called. Anyhow there's a thousand ways to debug this, all you have to do is find 1. good luck. – marinara Jul 21 '11 at 14:10
  • Thanks marinara. Doing my best right now, haha. – Jon Jul 21 '11 at 14:12
  • I just noticed that your parameters class has virtual methods. You most likely want to make your destructor virtual as well. (http://stackoverflow.com/questions/1123044/when-should-your-destructor-be-virtual) – R. Martinho Fernandes Jul 21 '11 at 14:31
  • There's no need to explicitly initialize all those CStrings to an empty string, their default constructor does that automatically. – Mark Ransom Jul 21 '11 at 14:38

1 Answers1

0

The problem is that you're making the pages as local variables in CSAPrefsDialog::OnInitDialog, and those variables are destroyed as soon as you leave the function. You should make them as member variables of your CSAPrefsDialog class. Everything else you're doing looks fine.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Hey Mark, I had originally tried doing that, but when I put FSC_Main fscMain into the header file, compiled, I kept getting a syntax error (missing ';' before fscMain) despite having the the correct includes, and not having any missing semicolons. I'll try again. Maybe I was doing something wrong. – Jon Jul 21 '11 at 14:40