放弃修改“公共"价值.多变的 [英] abend modifying value of "public" variable

查看:55
本文介绍了放弃修改“公共"价值.多变的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像我的最后一个问题没有证明我缺乏C ++专业知识一样,我现在将尝试超越自己.

在cdxuattqr.cpp中,我有以下代码:

As if my last question didn''t demonstrate my lack of C++ expertise, I will now try to outdo myself.

in cdxuattqr.cpp I have the following code:

if(m_pUAS->GetAndSetSpecs(1, text) > 0)
            m_szUAQRHelp = m_pUAS->m_szHelp;



在cdxuattspecs.h中,我有以下代码:



in cdxuattspecs.h I have the follwing code:

public:
	int		GetAndSetSpecs(int, char *);
	CString		m_szHelp;




在cdxuattspecs.cpp中,我有以下代码:




in cdxuattspecs.cpp I have the following code:

        char			CdxUAttSpecs_path[24]="C:\\tempDBspecfile.txt";	
	FILE			*CdxUAttSpecs_key;
	char			*CdxUAttSpecs_data=0;
        .
        .
        .

int CCdxUAttSpecs::GetAndSetSpecs(int iopt, char *string)
{
    int     knt=0, len=0, ichk=0, m, rc;
    char    *ptr, *ptr2, *ptr3;

    rc = -1;

    if(iopt)
    {
// init to default
        m_szHelp = "No help message";
    }

    if((CdxUAttSpecs_key=fopen(CdxUAttSpecs_path, "r")))
    {
        len = filelength(fileno(CdxUAttSpecs_key));
        ichk = len + 4;
        CdxUAttSpecs_data = (char *) setalloc(&zero, CdxUAttSpecs_data, &ichk);
        if(CdxUAttSpecs_data)
        {
            rc = 0;
            knt = fread(CdxUAttSpecs_data, len, 1, CdxUAttSpecs_key);
            fclose(CdxUAttSpecs_key);
                .
                .
                .
        }
    }
}



当cdxuattqr.cpp中的代码



When the code in cdxuattqr.cpp

if(m_pUAS->GetAndSetSpecs(1, text) > 0)
            m_szUAQRHelp = m_pUAS->m_szHelp;


被执行后,我会在尝试初始化GetAndSetSpecs中的公共变量m_szHelp时中止.

如果我注释掉


gets executed, I abend as soon as I try to initialize the public variable m_szHelp in GetAndSetSpecs.

If I comment out the line

m_szHelp = "No help message";


一切正常.为什么使用公共"变量而不是其他任何问题?

这是异常终止的文字:
cdx.exe中0x6129a1a3(mfc90d.dll)的首次机会异常:0xC0000005:访问冲突读取位置0xf8245b20.
cdx.exe中0x772a47db处的首次机会异常:0xC015000F:停用的激活上下文不是最近激活的上下文.
cdx.exe中0x77259dc1处的第一次机会异常:0xC0000005:访问冲突读取位置0xfeeefefe.
cdx.exe中0x77259dc1处未处理的异常:0xC0000005:访问冲突读取位置0xfeeefefe.
程序"[1156] cdx.exe:本机"已退出,代码-1073741819(0xc0000005).

这是失败代码的反汇编.变量"this"为零.这对我来说似乎是错误的.


everything works fine. Why the problems with the "public" variable and not any of the others?

This is the text of the abend:
First-chance exception at 0x6129a1a3 (mfc90d.dll) in cdx.exe: 0xC0000005: Access violation reading location 0xf8245b20.
First-chance exception at 0x772a47db in cdx.exe: 0xC015000F: The activation context being deactivated is not the most recently activated one.
First-chance exception at 0x77259dc1 in cdx.exe: 0xC0000005: Access violation reading location 0xfeeefefe.
Unhandled exception at 0x77259dc1 in cdx.exe: 0xC0000005: Access violation reading location 0xfeeefefe.
The program ''[1156] cdx.exe: Native'' has exited with code -1073741819 (0xc0000005).

This is the disassembly of the failing code. The variable "this" is zero. That seems wrong to me.

m_szHelp = "No help message";
100384B0  push        offset __real@40c3880000000000+0CCh (10C17F84h)
100384B5  mov         ecx,dword ptr [this]
100384B8  add         ecx,0ECh
100384BE  call        dword ptr [__imp_ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >::operator= (10C0224Ch)]

推荐答案

如果我记得上一个问题,则声明了
If I remember from your previous question, you declared
extern  CCdxUAttSpecs   *m_pUAS;    // <--- why unresolved???
extern  CCdxUAtts       *m_pUA;


因此它们是对象的指针(显然,您的引用指出了这一点).好的,这样就可以声明和定义它们了(一旦您解决了其他帖子中的问题).好的,它们是如何初始化"的?如何创建要指向的对象的实例?崩溃表明您正在使用NULL指针访问该对象,因此,如果您接受了CPallini的建议并使用了


and so they are pointers to objects (clearly your references indicate that). OK, so that gets them declared and defined (once you fixed the problems in your other post). OK, how are they "initialized"? How do you create an instance of the object that you are pointing to? The crash indicats that you are using a NULL pointer to access the object so if you took CPallini''s suggestion and used

CCdxUAttSpecs   *m_pUAS = NULL;

,则您尚未实例化该对象,只需将指针设置为零即可.

如果打算使用指向这些的指针,则必须在某个时候做一个新"操作.

then you have not instantiated the object, just set the pointer to zero.

If you intend on using pointers to these, you will have to do a "new" at some point.

CCdxUAttSpecs   *m_pUAS = new CCdxUAttSpecs;


当然,您可以静态分配它们(即,不使用指针),但是随后您必须将所有代码更改为使用".


Of course, you could statically allocate them (i.e., not use pointers) but then you''d have to change all the code to use "." instead of "->".


为什么不在CCdxUAttSpecs类的构造函数中而不是在其方法之一中初始化m_szHelp成员变量?
另外,
弯腰?没说什么真的发生了---崩溃!
Why don''t you initialise the m_szHelp member variable in the constructor of the CCdxUAttSpecs class, rather than in one of its methods?
In addition,
abend? Whay not say what really happend --- crash!


我认为问题可能是时机之一.
指针CCdxUAttSpecs * m_pUAS被声明为extern,这意味着它必须从某些外部代码段中分配.
可能在实际为指针分配了值之前执行了此代码.
因此,可以对代码的设计运行方式进行严格的桌面检查.
I think the question may be one of timing.
The pointer CCdxUAttSpecs *m_pUAS is declared extern, meaning that it has to be assigned from some external piece of code.
Maybe this code is being executed BEFORE that pointer is actually assigned a value.
So, a good hard desk-check of the way the code is designed to run may be in order.


这篇关于放弃修改“公共"价值.多变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆