Access violation writing location 0x00000020

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poko
    New Member
    • Dec 2009
    • 16

    #1

    Access violation writing location 0x00000020

    I have come across an exception that I have no idea about.

    " Unhandled exception at 0x00a2ac1b in SampleSocketCli ent.exe: 0xC0000005: Access violation writing location 0x00000020."

    Below is the snippet of relevant code. I started getting this error only after I added 'pComboLeg' & 'addAllLegs' in the tickPrice(). Can someone please help me out. I have googled alot but can't figure out what am I doing wrong. If you need more code than this then please ask.

    Code:
    [C.h file]
    struct ComboLeg
    {
    
       ComboLeg()
          :
          conId(0),
    	  ratio(0),
    	  openClose(0),
    	  shortSaleSlot(0)
       {
       }
    
       long    conId;
       long    ratio;
       CString action;
    
       CString exchange;
       long    openClose;
    
       long    shortSaleSlot; // 1 = clearing broker, 2 = third party
       CString designatedLocation;
    };
    
    struct Contract
    {
       Contract()
          : conId(0)
    	  , strike(0)
    	  , includeExpired(false)
    	  , comboLegs(NULL)
       {
       }
    
       long     conId;
       CString symbol;
       CString secType;
       CString expiry;
       double   strike;
       CString right;
       CString multiplier;
       CString exchange;
       CString primaryExchange;
       CString currency;
       CString localSymbol;
       bool includeExpired;
    
       CString comboLegsDescrip;
    
       typedef std::vector<ComboLeg*> ComboLegList;
       ComboLegList* comboLegs;
    };
    [C.h file end]
    
    
    [D.h file]
    
    typedef Contract::ComboLegList ComboLegList;
    Contract& getContract() { return *m_contract; }
    ComboLegList            m_comboLegs;
    std::auto_ptr<Contract>  m_contract;
    
    [D.h file end]
    
    
    [D.cpp file]
    
    m_contract(new Contract);
    m_contract->comboLegs = &m_comboLegs;
    
    [D.cpp file end]
    
    
    [E.cpp file]
    typedef long TickerId;
    
    void tickPrice( TickerId tickerId, TickType tickType, double price, int canAutoExecute)
    {
            Contract& contract1 = m_dlgOrder->getContract();
    			
    	ComboLeg* pComboLeg = new ComboLeg();
    	pComboLeg = NULL;
    
    	pComboLeg->conId = contract1.conId;
    	pComboLeg->ratio = 1;
    	pComboLeg->action = "BUY";
    	pComboLeg->exchange = "NYMEX";
    	pComboLeg->openClose = 0;
    	pComboLeg->shortSaleSlot = 0;
    	pComboLeg->designatedLocation = "";
    
    	ComboLegList* addAllLegs = new ComboLegList;
    	addAllLegs = NULL;
            addAllLegs->push_back(pComboLeg);
    	
    	contract1.secType = "BAG";
    	contract1.comboLegs = addAllLegs;
    }
    [E.cpp file end]
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    D.h file]

    typedef Contract::Combo LegList ComboLegList;
    Contract& getContract() { return *m_contract; }
    ComboLegList m_comboLegs;
    std::auto_ptr<C ontract> m_contract;

    [D.h file end]
    One weakness is right here. This header file has code in it. That means every time this header is included you get another m_combolegs and another m_contract and another copy of getContract().


    Your headers must contain only declarations and not definitions.

    DO not use global variables.
    Read this: http://bytes.com/topic/c/insights/73...obal-variables

    The only thing that should be in this header is the typedef. And even that shouldn't; be there since ComboLegList is a member of a struct.

    These structs should be classes with no public data members.

    All of this leads to no encapsulation. That means you problems can occur anywhere in the code rather than only in the functions that deal with particular data members. If you make member functions responsible for access to and from data members, if those membrs get messed up then it the error must ion in those functions. Otherwise, the error can be anywhere in the code.

    If you can't redesign this code, then start setting breakpoints and stepping though with your debugger. Pay particular attention to the addresses of your global variables to be sure you have only one of each name.

    Comment

    Working...