Jump to content

Variable manager bad output


ParaToxic
 Share

Recommended Posts

Hey guy's I have a problem with a part of my new debuging console.The idea is to make a console with a listbox, where you can insert variables and the name, which were shown in the console.

Something like:

IConsole* console = new Console();
console->Initialize();
int b = 65;
console->AddItem("b",B);
console->Draw();
b = 45656;
console->Draw(); // The console shows 45656 now

 

In a second little projekt in VS I have the same as a little program to test, there it work's fine but here I get a value of -8445360 ( not a random undefined nummber, because it's allways the same ). I don't know, maybe someone can help me with that:

 

#ifndef ICONSOLE_H_
#define ICONSOLE_H_
#pragma once
#include <windows.h>
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include "IComponent.h"
#include "GlobalDefine.h"
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' "\
"version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK IConsole_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
//-------------------------------------------------------------------------------//
class IVariable {
 public:
  virtual void printTo(std::string& out) const = 0;
};

template <typename T>
class VariableImpl : public IVariable {
 public:
   explicit VariableImpl(T* managed = NULL) : m_managed(managed) {}
   void setManaged(T* managed) { m_managed = managed; }
   virtual void printTo(std::string& out) const {
  std::stringstream ss;
  ss << *m_managed;
  out = ss.str();
   }
 private:
   T* m_managed;
};
template <typename T>
boost::shared_ptr<IVariable> make_variable(T const& var) {
 return boost::shared_ptr<IVariable>(new VariableImpl<T const> (&var));
}
//--------------------------------------------------------------------------------------//
class IConsole : public IComponent
{
public:
   IConsole(void);
   ~IConsole(void);
   void Update();
   IResult Initialize();
   IResult Terminate();
   void Test() { MessageBoxA(0,"Hallo",0,0); }
   //--------------------------------------------------------//
 template <typename T>
 void AddItem(std::string name, T const& v) { m_variables[name] = make_variable(v); _listItems++; }
 //--------------------------------------------------------//

private:

   //-------------Windows---------//
   HWND _consoleWindow;
   HWND _listWindow;
   MSG msg;
   int _listItems;
   void UpdateList(std::string content);
   void DeleteList(int items);
   //-----------------------------//
   //-----------------------------//
   std::map<std::string, boost::shared_ptr<IVariable> > m_variables;
   //-----------------------------//
   void Create() { MessageBoxA(0,"Created",0,0);}
};


#endif

 

 

#include "IConsole.h"

IConsole::IConsole()
{
}
IConsole::~IConsole()
{
}

IResult IConsole::Initialize()
{

   HINSTANCE hinst = GetModuleHandle(NULL);
   WNDCLASS wndclass;
   wndclass.style = CS_HREDRAW | CS_VREDRAW;
   wndclass.lpfnWndProc = IConsole_WndProc;
   wndclass.cbClsExtra = 0;
   wndclass.cbWndExtra = 0;
   wndclass.hInstance = hinst;
   wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
   wndclass.lpszMenuName = NULL;
   wndclass.lpszClassName = TEXT("Console");
   if (!RegisterClass(&wndclass)) {
    return IRESULT_ERROR;
   }
   //------------------------Erstelle Fenster-------------------//
   _consoleWindow = CreateWindow(TEXT("Console"),
							  TEXT("Console"),
							  WS_OVERLAPPEDWINDOW,
							  CW_USEDEFAULT,
							  CW_USEDEFAULT,
							  500,
							  400,
							  NULL,
							  NULL,
							  hinst,
							  NULL);
   if(!_consoleWindow)
    return IRESULT_ERROR;
   ShowWindow(_consoleWindow,SW_SHOWNORMAL);
   UpdateWindow(_consoleWindow);
   //------------------------Erstelle Listbox---------------------//
   // Die Größe der Listbox wird an die größe des Konsolenfensters angepasst
   // , da sich dieses nicht in der Größe verändert und eine dynamische
   // Umformung nicht notwendig ist
   //
 IConsole::_listWindow = CreateWindow(TEXT("LISTBOX"),
						    TEXT("Console"),
						    WS_CHILD | WS_VISIBLE| WS_BORDER | LBS_DISABLENOSCROLL | WS_VSCROLL   ,
						    0,
						    0,
						    484,
						    365,
						    _consoleWindow,
						    NULL,
						    hinst,
						    NULL);

 _listItems = 0;
   return IRESULT_OK;
}
IResult IConsole::Terminate()
{
   return IRESULT_OK;

}
void IConsole::Update()
{
   DeleteList(_listItems);

   for(std::map<std::string, boost::shared_ptr<IVariable> >::const_iterator it = this->m_variables.begin(); it != this->m_variables.end(); ++it) {
    std::stringstream ss;
    std::string st;
    it->second->printTo(st);
    MessageBoxA(0,st.c_str(),0,0);
    ss << (it->first) << " = " << st << std::endl;
    UpdateList(ss.str());
   }


   if(GetMessage(&msg, NULL, 0, 0))
   {
	    TranslateMessage(&msg);
	    DispatchMessage(&msg);
   }
}
void IConsole::UpdateList(std::string content)
{
   SendMessage(_listWindow, LB_ADDSTRING,0,(LPARAM)content.c_str());
}
void IConsole::DeleteList(int items)
{
   for(int i = items; i > 0; i--)
   {
    SendMessage(_listWindow,LB_DELETESTRING,(WPARAM)i,NULL);
   }
}
LRESULT CALLBACK IConsole_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch (msg)
   {
   case WM_CREATE:

   case WM_DESTROY:
    PostQuitMessage(0);
    break;
   default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
   }
   return 0;
}

 

Thanks for advance

Link to comment
Share on other sites

I assume you meant this (console->AddItem("b",cool.pngwink.png to be this (console->AddItem("b",cool.pngwink.png?

 

 

It looks like AddItem() needs a type, but in your first example I don't see that. Does it really look like:

 

console->AddItem<int>("b", cool.png;

 

 

There shouldn't be a mystery as to where it's happening as you can step through each line to see what values are what.

Link to comment
Share on other sites

I looked at that.Add at some lines MessageBoxA.... but I have everywhere this value ( it starts at the printTo function )

 

Here is the code from the easy project and it is the same and works fine :/

 

#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <stdlib.h>

class Variable {
 public:
   virtual void printTo(std::string& out) const = 0;
};

template <typename T>
class VariableImpl : public Variable {
 public:
   explicit VariableImpl(T* managed = NULL) : m_managed(managed) {}
   void setManaged(T* managed) { m_managed = managed; }
   virtual void printTo(std::string& out) const {
  std::stringstream ss;
  ss << *m_managed;
  out = ss.str();
   }
 private:
   T* m_managed;
};
template <typename T>
boost::shared_ptr<Variable> make_variable(T const& var) {
 return boost::shared_ptr<Variable>(new VariableImpl<T const> (&var));
}
class VariableManager {
 public:
 template <typename T>
 void addVariable(std::string name, T const& v) { m_variables[name] = make_variable(v); }
 std::string Out() {
  std::string out;
    for(std::map<std::string, boost::shared_ptr<Variable> >::const_iterator it = this->m_variables.begin(); it != this->m_variables.end(); ++it) {
  std::stringstream ss;
  std::string st;
  it->second->printTo(st);
  ss << (it->first) << " = " << st << std::endl;
  out = ss.str();
   }
   return out;
 }
 private:
   std::map<std::string, boost::shared_ptr<Variable> > m_variables;
};

int main() {
 VariableManager a;
 int i = 54;
 a.addVariable("i",i);

 std::string b;
 b = a.Out();
 std::cout  << a.Out();

 getchar();
}

Link to comment
Share on other sites

Do you have the int main() from the program that doesn't work? Or an example with a basic int main() if the major program is to big.

 

As Ruki suggests, if in your bottom example i goes out of scope (because maybe you define it in a function that exists, then you might be pointing to a not valid memory location.

Link to comment
Share on other sites

But the exmaple at the bottom works fine, no problems.I can't understand why the other doesn't work.It is exactly the same, just that the main projekt is a win32 project.

 

Hmmmm I can't understant that. At my first test I tried to use an other file with this classes and implement them to my console class, but I became 2 linker errors, that the class std::basic_string(....) is allready defined in the Variable class ( look at the second example ).

 

Now I have the stuff direct in my console class.

 

EDIT: I tested it with winmain and it works fine.I realy don't know where the problem is.....

Link to comment
Share on other sites

Sorry for tripplepost but now I think I have the problem. When I create a new instance of the VariableManager class in the IConsole Initialize function , add in the same function a variable and call the output function everything works fine, but when I call the output function in a other function like Update, I become this bad values back.

 

Can you imagine why this is so ??

Link to comment
Share on other sites

I have the problem now.The problem is that if you create a local variable in a function, first the entered template points to it.But when I try to call the Update or Output function of the class in a other function it points to a not existing variable----> bad output.

 

Now I can fix that or let it ,because I wan't to display only memebr variables of a class :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...