473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User created class in MFC going out of scope???

175 New Member
I'm having trouble using MFC in Visual Studio 6. The problem I'm having is, say I have a text box that information is typed into. Once a button is clicked, this information is passed into code in a class ( that I created and added to the project ). This works fine. Now, there's a second button, and a second text box. When that second button is clicked, the info that was sent into my code should come out ( via another function in my class ) in that second text box. It's not happening. The text box is being filled with jumbled garbage.

One thing I did notice, is that if I perform BOTH functions inside 1 button click, it comes out fine. This makes me think that my class, my code, is going out of scope and losing it's values.

The problem is that I need my code to retain it's values. This is the first time I've ever used MFC, and I've tried everything I can think of, but nothing is working. I've never been this frustrated, and nothing I've found addresses this. It's probably something silly I'm missing, but I need help.

If what I'm saying isn't clear, please let me know, and I'll give more details. Any help is greatly appreciated.
May 7 '08 #1
6 1827
Banfa
9,065 Recognized Expert Moderator Expert
I'm having trouble using MFC in Visual Studio 6. The problem I'm having is, say I have a text box that information is typed into. Once a button is clicked, this information is passed into code in a class ( that I created and added to the project ). This works fine. Now, there's a second button, and a second text box. When that second button is clicked, the info that was sent into my code should come out ( via another function in my class ) in that second text box. It's not happening. The text box is being filled with jumbled garbage.
The text is not passed into a class, it is passed into an instance of the class.

The class is the type of the thing, the instance is the thing itself.

Without seeing any code it sounds like you do not have an instance of your class that persists between calls to the button handlers. It sounds like you are creating the instance as an automatic variable on the stack. An instance of this sort would be destructed when the function enters resulting in the 2 button click handler functions operating on separate instances of the class.

You need to place an instance of the class (or a pointer to an instance that is new'd) in the class that contains the button handler methods.
May 7 '08 #2
manontheedge
175 New Member
It sounds like you are creating the instance as an automatic variable on the stack. An instance of this sort would be destructed when the function enters resulting in the 2 button click handler functions operating on separate instances of the class.

You need to place an instance of the class (or a pointer to an instance that is new'd) in the class that contains the button handler methods.
thanks for the reply,

that is what I thought was the problem, where I was declaring the instance of my class. I'll just put some of the code up...


this is my class ( i've stripped everything out but 2 functions to keep testing it )

Expand|Select|Wrap|Line Numbers
  1. #ifndef INFO_H
  2. #define INFO_H
  3.  
  4. class info
  5. {
  6.  
  7. private:
  8.     char* name;    
  9.  
  10. public:
  11.     void addName( char* n );
  12.     char* viewNames();
  13. };
  14.  
  15. #endif

my project name is "book", so this is the header file for the GUI part ... the code that is bold is what i've added ... THIS is where I declare the instance of my class.

Expand|Select|Wrap|Line Numbers
  1. // bookDlg.h : header file
  2. //
  3.  
  4. #if !defined(AFX_BOOKDLG_H__B2E13E72_531A_4911_BEA6_D9116C6D7D45__INCLUDED_)
  5. #define AFX_BOOKDLG_H__B2E13E72_531A_4911_BEA6_D9116C6D7D45__INCLUDED_
  6.  
  7. #if _MSC_VER > 1000
  8. #pragma once
  9. #endif // _MSC_VER > 1000
  10.  
  11.  
  12. #include "info.h"
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CBookDlg dialog
  16.  
  17. class CBookDlg : public CDialog
  18. {
  19. // Construction
  20. public:
  21.     CBookDlg(CWnd* pParent = NULL);    // standard constructor
  22.  
  23. // Dialog Data
  24.     //{{AFX_DATA(CBookDlg)
  25.     enum { IDD = IDD_BOOK_DIALOG };
  26.     CString    m_first;
  27.     CString    m_last;
  28.     CString    m_email;
  29.     int        m_index;
  30.     int        m_index2;
  31.     info i;
  32.     //}}AFX_DATA
  33.  
  34.     // ClassWizard generated virtual function overrides
  35.     //{{AFX_VIRTUAL(CBookDlg)
  36.     protected:
  37.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  38.     //}}AFX_VIRTUAL
  39.  
  40.     // Implementation
  41. protected:
  42.     HICON m_hIcon;
  43.  
  44.     // Generated message map functions
  45.     //{{AFX_MSG(CBookDlg)
  46.     virtual BOOL OnInitDialog();
  47.     afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
  48.     afx_msg void OnPaint();
  49.     afx_msg HCURSOR OnQueryDragIcon();
  50.     afx_msg void OnButton1();
  51.     afx_msg void OnButton3();
  52.     //}}AFX_MSG
  53.     DECLARE_MESSAGE_MAP()
  54. };
  55.  
  56. //{{AFX_INSERT_LOCATION}}
  57. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  58.  
  59. #endif // !defined(AFX_BOOKDLG_H__B2E13E72_531A_4911_BEA6_D9116C6D7D45__INCLUDED_)

this is the definition file for the GUI part ... the code that is bold is what i've added

Expand|Select|Wrap|Line Numbers
  1. // bookDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "book.h"
  6. #include "info.h"
  7. #include "bookDlg.h"
  8.  
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CAboutDlg dialog used for App About
  18.  
  19. <snip CAboutDlg implementation>
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CBookDlg dialog
  23.  
  24. CBookDlg::CBookDlg(CWnd* pParent /*=NULL*/)
  25.     : CDialog(CBookDlg::IDD, pParent)
  26. {
  27.     //{{AFX_DATA_INIT(CBookDlg)
  28.     m_first = _T("");
  29.     m_last = _T("");
  30.     m_email = _T("");
  31.     m_index = 0;
  32.     m_index2 = 0;
  33.     //}}AFX_DATA_INIT
  34.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  35.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  36. }
  37.  
  38. <snip CBookDlg functions that are not relevent to problem>
  39.  
  40. void CBookDlg::OnButton1() 
  41. {
  42.     UpdateData( true );    
  43.  
  44.     char temp[50];
  45.     strcpy( temp, m_first );
  46.     i.addName( temp );
  47.  
  48.     UpdateData( false );    
  49. }
  50.  
  51.  
  52. void CBookDlg::OnButton3()
  53. {
  54.     UpdateData( true );
  55.  
  56.     m_last = i.viewNames();
  57.  
  58.     UpdateData( false );
  59. }
m_first is the first text box, m_last is the second text box ( just to clarify ).


hopefully, that makes it more clear. Again, I appreciate the help.
May 8 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
This looks correct which suggest the error is in your implementation of class info.
May 8 '08 #4
manontheedge
175 New Member
this is the declaration of my class
Expand|Select|Wrap|Line Numbers
  1. #ifndef INFO_H
  2. #define INFO_H
  3.  
  4. class info
  5. {
  6.  
  7. private:
  8.     char* name;    
  9.  
  10. public:
  11.     void addName( char* n );
  12.     char* viewNames();
  13. };
  14.  
  15. #endif

this is the definition of the class
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include "info.h"
  3.  
  4. void info::addName( char* n )
  5. {
  6.     name = n;
  7. }
  8.  
  9. char* info::viewNames( )
  10. {
  11.     return name;
  12. }

here is where my class is being called ...the only thing it could possibly be is the "UpdateData", but I don't THINK that has anything to do with it (?), but again, if I call both functions of my class in "OnButton1()" it works fine, telling me my class is okay...
Expand|Select|Wrap|Line Numbers
  1. void CBookDlg::OnButton1() 
  2. {
  3.     UpdateData( true );    
  4.  
  5.     char temp[50];
  6.     strcpy( temp, m_first );
  7.     i.addName( temp );
  8.  
  9.     UpdateData( false );    
  10. }
  11.  
  12.  
  13. void CBookDlg::OnButton3()
  14. {
  15.     UpdateData( true );
  16.  
  17.     m_last = i.viewNames();
  18.  
  19.     UpdateData( false );
  20. }
Is it possible there's some setting I've missed? Like I said before, this is my first time using MFC. I've looked over this, and messed with it for hours, and can't find a single thing wrong with it. Maybe there's something I need to do with the project settings ... that's the ONLY thing I can think of. Any ideas?
May 8 '08 #5
Banfa
9,065 Recognized Expert Moderator Expert
Nope you have an implementation problem in your class info.

You are copying the pointer not what is pointed to, however where you are calling your instance of the class you are passing a pointer to the array temp which (as the name suggests) only exists temporarily as it is on the stack. As soon as the function exits that variable ceases to exist and the data is used for something else.

The instance of you class is left pointing at the place temp used to exist.

What you need to do is inside info allocate memory for storage of the supplied string using new, making sure that you don't cause any memory leaks and that you release the memory when the instance is destructed and that if the instance is reused there is enough memory for the new usage.

This memory will persist until you release it because it is allocated from the heap not the stack.
May 8 '08 #6
manontheedge
175 New Member
Banfa, thank you so much. Apparantly I'm a little more rusty on dealing with memory in C++ than I thought. I'll go back and read up on what I've forgotten. I really appreciate the help. Thank you, that was definately the problem.
May 8 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3179
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
2
3006
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
7
2899
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
19
6524
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
7
1718
by: MariusI | last post by:
Are objects implicitly stored in the TLS of the currently running thread? When creating multithreaded applications i get errors when accessing data from a different thread than the thread used to...
9
2330
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
10
2691
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
9
1971
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
11
1660
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.