Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:03:49+00:00 2026-06-07T07:03:49+00:00

is the following code 100% correct? I’m coding a game and CItemElem is a

  • 0

is the following code 100% correct? I’m coding a game and CItemElem is a class that holds Item informations, such as power etc. I want to store pointers from the items into an instance of another class named CChatLink. Please have a look:

[.H]

#pragma once

class CChatLink
{
private:
    CChatLink(){}; //no new usage or global object allowed
    ~CChatLink(){}; 
public:
    BOOL InsertChatLink( TCHAR* szText, CItemElem *pItemElem );
    map<std::string,CItemElem*>m_mChatLink;
    static CChatLink* GetInstance( void )
    {
        static CChatLink pObj;
        return &pObj;

    }

};

[.cpp]

#include "StdAfx.h"
#include "Item.h"
#include "CChatLink.h"

BOOL CChatLink::InsertChatLink( TCHAR *szText, CItemElem* pItemElem )
{
    if( pItemElem && szText )
    {
        std::string szInsert( szText );
        CItemElem *pItem = new CItemElem; //as far as I know, it must be allocated on the heap to be inserted ^^
        pItem = pItemElem;
        m_mChatLink.insert( make_pair( szInsert, pItem ) );
        return TRUE;
    }
    return FALSE;
}

Can I store the std::string into the map that way?

(I’m currently studying C++ so please go easy on me.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:03:51+00:00Added an answer on June 7, 2026 at 7:03 am

    Yes you have a memory leak right here:

    CItemElem *pItem = new CItemElem;
    pItem = pItemElem;
    

    You just lost the address to where the object was allocated. Why are you doing this anyways if the item to be inserted is being passed as an argument? It is not clear from your code.

    Is this what you meant instead?:

    BOOL CChatLink::InsertChatLink( TCHAR *szText, CItemElem* pItemElem )
    {
        if( pItemElem && szText )
        {
            std::string szInsert( szText );
            m_mChatLink.insert( make_pair( szInsert, pItemElem ) );
            return TRUE;
        }
        return FALSE;
    }
    

    EDIT:

    It sounds like pItemElem is a pointer to an object that has been allocated on the stack. You could either change m_mChatLink to a std::map<std::string, CItemElem> (not a CItemElem*) OR this:

    CItemElem pItem = new CItemElem;
    *pItem = *pItemElem;
    m_mChatLink.insert(make_pair(szInsert, pItem));
    

    You will need to delete all these CItemElems in your destructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you tell me if the following code is 100% correct? Expecially the dealloc
How can the following code be modified to run my insert statements 100 at
I have the following code and instead of height(300), height() = document height-100 $(.bar).height(XXX);
I have the following code: void foo() { vector<double> v(100,1); // line 1 //
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
I have the following two lines of code: var BadResult = (100).ToString(B, new CustomFormatter
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
I am facing the following code: char buf[100]; char buf2[100]; strcpy( buf, áéíóúç );
So i have the following code on my img's width:100%; height: auto; However IE6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.