Exit code 3

Started by
12 comments, last by HinataHyuga 17 years, 3 months ago
As I said before I think you will have problems in other parts of your code. Lets just go crazy can you post all the code you have :)

[edit]Note to other members. Seeing as VS express is suggested to all new comers, is there an article on the board about debugging in VS?
[edit edit]Superpig explains debugging
Advertisement
sure here we go:
test.cpp which contains the application
#include<windows.h>#include"EObject.h"#include"EBitmap.h"#include"EText.h"bool appdone = false;const string classname = "cory";const string windowname = "window";EBitmap bit("fuck",500,500);LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, PSTR cmdLine, int iCmd){	WNDCLASS wc;	wc.cbClsExtra = 0;	wc.cbWndExtra = 0;	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);	wc.hCursor = LoadCursor(NULL,IDC_ARROW);	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);	wc.hInstance = hInstance;	wc.lpfnWndProc = WndProc;	wc.lpszClassName = classname.c_str();	wc.lpszMenuName = NULL;	wc.style = CS_VREDRAW|CS_HREDRAW;	RegisterClass(&wc);	HWND hwnd = CreateWindow(classname.c_str(),windowname.c_str(),WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);	ShowWindow(hwnd,iCmd);	UpdateWindow(hwnd);	MSG msg;	while(!appdone){		if(GetMessage(&msg,NULL,0,0)){			TranslateMessage(&msg);			DispatchMessage(&msg);		}		else{		}	}	EObject::Cleanup();	UnregisterClass(classname.c_str(),hInstance);	return 0;}LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){	switch(iMsg){		case WM_DESTROY:			PostQuitMessage(0);			appdone = true;			break;		case WM_PAINT:			PAINTSTRUCT ps;			HDC hdc = BeginPaint(hwnd,&ps);			//bit1.Draw(250,250,hwnd);			EndPaint(hwnd,&ps);			break;	}	return DefWindowProc(hwnd,iMsg,wParam,lParam);}

EObject.h
#ifndef EOBJECT_H#define EOBJECT_H#include<list>#include<fstream>using namespace std;class EObject{private:	static list<EObject *> LiveObjects;	static list<EObject *> DeadObjects;	int refcount;protected:	EObject();	virtual ~EObject();public:	int GetRefCount();	void AddRef();	void Release();	static void CollectGarbage();	static void Cleanup();};#endif

EObject.cpp
#include"EObject.h"list<EObject *> EObject::LiveObjects;list<EObject *> EObject::DeadObjects;EObject::EObject(){	LiveObjects.push_back(this);	refcount = 0;}EObject::~EObject(){}void EObject::AddRef(){	++refcount;}void EObject::Release(){	--refcount;	if(refcount<=0){		LiveObjects.remove(this);		DeadObjects.push_back(this);	}}void EObject::CollectGarbage(){	for(list<EObject *>::iterator it = DeadObjects.begin();it != DeadObjects.end();it++){		EObject *obj = *it;		delete obj;	}}void EObject::Cleanup(){	ofstream file("debug.txt",ios::app);	file<<"DeleteingObjects"<<endl;	if(LiveObjects.size() > 0)		for(list<EObject *>::iterator i = LiveObjects.begin(); i != LiveObjects.end();){		EObject* o = *i;//dereference the iter		i = LiveObjects.erase(i);//remove the object from the list and increment the iter		delete o;//recovery memory	}	if(DeadObjects.size() > 0)	for(list<EObject *>::iterator c = DeadObjects.end(); c != DeadObjects.begin();c--){		file<<"Deleting dead object!"<<endl;		EObject *obj = *c;		delete obj;	}	if(LiveObjects.empty()){		file<<"LiveObjects deleted successfully."<<endl;	}	else{		file<<"Error deleting live objects!"<<endl;	}	if(DeadObjects.empty()){		file<<"DeadObjects deleted successfully."<<endl;	}	else{		file<<"Error deleting dead objects!"<<endl;	}	file<<"Deletion complete!"<<endl;	file.close();}int EObject::GetRefCount(){	return refcount;}

EBitmap.h
#ifndef EBITMAP_H#define EBITMAP_H#include<windows.h>#include"EObject.h"class EBitmap : public EObject{private:	int width;	int height;	HBITMAP bitmap;public:	int GetWidth();	int GetHeight();	void Draw(int x, int y, HWND hwnd);	EBitmap(string location, int width, int height);	~EBitmap();};#endif

EBitmap.cpp
#include"EBitmap.h"int EBitmap::GetWidth(){	return width;}int EBitmap::GetHeight(){	return height;}void EBitmap::Draw(int x,int y, HWND hwnd){	HDC hdc = GetDC(hwnd);	HDC idc = CreateCompatibleDC(hdc);	SelectObject(idc,bitmap);	BitBlt(hdc,x,y,x+width,y+height,idc,0,0,SRCCOPY);	DeleteDC(idc);	ReleaseDC(hwnd,hdc);}EBitmap::EBitmap(const string location,const int iwidth,const int iheight){	width = iwidth;	height = iheight;	bitmap = (HBITMAP)LoadImage(NULL,location.c_str(),IMAGE_BITMAP,width,height,LR_LOADFROMFILE);}EBitmap::~EBitmap(){}

also there is EText but it contains nothing so i have not posted its source
Thanks,
Hinata
Looks like I'm going to have read the article and see how the memory management is working as it pushed the this pointer onto the list.

Ok the article goes on to explain smart pointers which are used to wrap the objects and handle the scope of them. The problem you are having is that you create the bitmap in global scope on stack and then you are deleting it in the function which is throwing the error, the tracking of memory objects is only for memory which is created with the keyword "new" so if you want to wrap it up in the memory manager create it in main using
Quote:

int main(...)
{
EBitmap* bit = new EBitmap("duck",500,500);
...
}

[edit]
You either need to add incorporate the reference counting the article goes on to take about( I think you really need to read all the article before using it) or do it your self by calling the member function ::AddRef and ::Release and checking this as the article code shows.


[Edited by - dmail on January 2, 2007 8:51:16 PM]
i will test your solution in just a minute but the reason i didn't go on to try the rest of the article is it has things i dont understand like templates functors and so on. I need to read a book on advanced c++ topics before i can do that section.
Thanks for the input i'll let you know if it works.
-Hinata
EDIT: YES IT WORKED :-) Now i have to figure out how to draw my bitmap without it being in global scope lol.
I'm so confussed.
-Hinata

This topic is closed to new replies.

Advertisement