CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Feb 2012
    Posts
    10

    [RESOLVED]Memory Leak inside Static Library

    Hi guys I'm new here and I'm very thankful because this forum helps me a lot to answer some question in mind when it comes c++ codes.

    I need your suggestions on my leak problem in the game that i'm creating. I already found cause of the leak.. it is the vector of pointers which globally declare inside the library(static library).

    //******************** Simple Pseudo Code of the game **********************
    //Inside the library
    //It was extern in Button.h
    extern vector<Button*>ButtonList

    //and it was used inside the SetupButton member functon of the Button class
    Class Button
    {
    void SetupButton()
    }

    //And it was declare at the Button.cpp as
    vector<Button*>ButtonList

    //It was use inside the SetupButton member function so that all available buttons in the game will be added in the list
    void Button::SetupButton()
    {
    ... //setup the property of the button
    ...//setup the position
    ...//etc.

    //then add to the list
    ButtonList.push_back( this );

    }

    And in the game...
    #include "Button.h"
    Game::Game()
    {
    Button btnStartButton, btnExitButton;
    btnStartButton.SetupButton();
    btnExitButton.SetupExitButton();
    }

    Game::~Game()
    {
    ButtonList.clear();

    ... Delete some objects used
    }

    Game* pGame
    void Main()
    {
    pGame = new Game()
    ...
    ...
    pGame->RunGame();

    delete pGame;
    return;
    }

    Basically this how the setup made .. the game is running and its ok except there is a reported leak.

    I'm wondering if what is the best approach to fix the problem. I cant use delete on every elements because they are not dynamically allocated. One solution I made and it will definitely solved the leak is to make the ButtonList vector as member of a class but the changes in the game and in the library will be huge ... Im asking you guys if you have better idea that can save time and lines of codes.

    Im thinking if i can use a singleton class as a based class of the button and put the ButtonList vector as static member of this class but im not so familliar with singleton class so i dont know if its possible


    Any inputs will be appreciated ...sorry for my bad English

    Thanks in Advance
    Last edited by himitsujanai; February 17th, 2012 at 09:38 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured