|
Thanx for your answer.
I do call a procedure in the last while statement wich is using pointers. So I'm afraid I do not delete these pointers properly.
While I check the system performance I can see that I use more and more virtual memory. It stops at about 2.1GB (all used) and then fills the RAM. Thereafter the program crashes.
I do have about 100GB frre space on the hard drive.
I talked to a friend and he proposed that I should use case statements instead. What would be the differense?
/Martin
Martin_j
|
|
|
|
|
I think... the you should delete your pointers properly!
|
|
|
|
|
martin_j wrote:
So I'm afraid I do not delete these pointers properly.
Properly or not, are you deleting them at all? Remember that every new must have a matching delete .
martin_j wrote:
I should use case statements instead. What would be the differense?
Instead of what? The switch/case statement is synonymous with the if/else statement.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Sorry, I meant do/until.
I do delete all pointers as far as I can see.
Is there an easy way to debug the program and see if there is a memory leak?
/Martin
Martin_j
|
|
|
|
|
if you use MFC you can just run your app for a short
time with the Debuger attached, then exit it
and in the debug-output-window you should see
your mem leaks
|
|
|
|
|
Thanx
Martin_j
|
|
|
|
|
Even if you aren't using MFC, you can still debug memory leaks with functions like:
_CrtMemCheckpoint()
_CrtMemDifference()
_CrtMemDumpStatistics()
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Great thanx
Martin_j
|
|
|
|
|
Oopss, sorry i accidently clicked on 1 instead of 5.
I was looking for something like this all the time.
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|
Hi I have the following problem:
I have an executable that loads dlls (MFC extension dll´s).
All the dll´s are based on a parent class with various functions. To name a few
BOOL mfb_open_database (LPCTSTR);
CString mfx_get_module_name();
CString mfx_get_item_text (LPVOID);
The functions are called from the main application using a pointer to the dll class..
x_txt = px_dll->mfx_get_item_text((LPVOID)something);
The functions in the dll´s are all with AFX_MANAGE_STATE(..) because I´m loading things from the (DLL´s) resources
The funny thing is only the mfx_get_module_name() does not work, all the others do.
Tracing through this (I have added the debug symbols to the release mode of a dll and the exe)
It comes about when the constructors for the CString objects are called. As far as I can tell, the DLL creates a CString object to be returned. Instead of a "Deep" copy, only the reference of it´s m_pchData is copied to the CString Object in the exe, and when the destructor for the CString Object in the exe is called, the MFC crashes and says that the Object that is to be destroyed is not in the "correct" heap. I have tried adding
::LockBuffer() to the CString object that is created in the DLL as the returned object but to no avail.
I have checked all the project settings etc. and can´t find anything wrong.
This function worked perfectly well up until yesterday, until my devstudio crashed a few times. I´ve checked all the settings in Project setings but can´t find anything un toward. Has anybody any idea´s I´m at my wits end....
Thanks in advance
Phil
bum... and I thought I´d got rid of all the bugs
|
|
|
|
|
are the dlls AND the app the same build type??
Release or Debug? I spent some days on a realy anoying bug. My App was Release Build and the DLL
was Debugbuild and it crashes all the time!!
solution-> use the same build type.
|
|
|
|
|
Yes, same build type. The really stupid thing is, that before the DevStudio crash everything worked fine
bum... and I thought I´d got rid of all the bugs
|
|
|
|
|
sure?
I also thought all the time, my DLLs and the
App are the same build, BUT somewhere deep was a silly path to the wrong directory with the wrong DLLs.
don't know other reason.. sory.
|
|
|
|
|
Yes I´ve checked.. I´ll have another look at the settings in a text editor, but in the IDE everything seems to be ok. And thanks for helping...
Phil
bum... and I thought I´d got rid of all the bugs
|
|
|
|
|
I have finally found the problem. In one of my base classes I have over written the OnDraw() and OnEraseBkgnd() to draw a custom background. Instead of using the MFC message entry macro,
ON_WM_ERASEBKGND(), ON_WM_PAINT(),
I used the API one: ON_MESSAGE(WM_PAINT, ...) etc.
This all worked fine in debug mode, but crashed in release.
Now everything works fine
bum... and I thought I´d got rid of all the bugs
|
|
|
|
|
Hello there,
Once a dll is loaded into the address-space of an application is there a delay in the execution of code from the DLL itself? I mean is code from the DLL executed slower then when that same code would have been compiled in the application itself?
Regards
|
|
|
|
|
Calls within the dll itself will be same as calls within the exe only.
But may be the calls between the exe and the dll could be slow coz the calls go through the CALL Gates i.e far calls.
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|
There will be no noticeable difference, and certainly not anything worth bothering about. The kernel is a DLL after all!
|
|
|
|
|
Hi guys,
This is my first post so excuse the low level of expertise, I've only been playing with DirectDraw for just over a week.
Anyway, the thing I want to do is read several bitmap files from a directory and display them consecutivley (like a movie). I've been searching the internet for useful files and have been using several tutorials and code snippets to get the job done. I basically started with this project (http://www.codeproject.com/directx/basicdd.asp) and edited from there. I got the bitmaps to play full screen and then made the transition to windowed mode. I followed the tutorials and everything actually works, the only thing is that I get garbage on my screen! I get a pinkinsh/whiteish mix of a distorted plain and everey now and then I can seet he bitmap which is loaded flicker on the screen but only for an extremely short time. I'm wondering if anyone can spot my mistake, here's my source:
BasicDD.cpp
///////////////////////////////////////////////////////////////////
// BasicDD.cpp : Defines the entry point for the application. //
/////////////////////////////////////////////////////////////////
//Includes
#include "stdafx.h"
#include <ddraw.h>#include "csurface.h"
#include "resource.h"
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
//Declarations
HWND hMainWnd;
HINSTANCE hInst;
LPDIRECTDRAW7 DD = NULL; // DirectDraw object
LPDIRECTDRAWSURFACE7 FrontBuffer = NULL; // DirectDraw frontbuffer surface
LPDIRECTDRAWSURFACE7 BackBuffer = NULL; // DirectDraw backbuffer surface
LPDIRECTDRAWCLIPPER Clipper = NULL;
cSurface BitmapSurface;
long i = 0;
string FileName;
ostringstream FileNameStream;
int ScreenWidth = 720;
int ScreenHeight = 576;
//Prototypes
HWND InitWindow(int iCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void ProcessIdle(char *File);
int InitDirectDraw();
void CleanUp();
//Main class
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Declarations
hInst = hInstance;
hMainWnd = InitWindow(nCmdShow);
//Error checking
if(!hMainWnd)
return -1;
if(InitDirectDraw() < 0)
{
CleanUp();
MessageBox(hMainWnd, "Could start DirectX engine in your computer. Make sure you have at least version 7 of DirectX installed.", "Error", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
//Create a Bitmap surface
BitmapSurface.Create(DD, ScreenWidth, ScreenHeight);
//Loop while the program runs
while( TRUE )
{
//Declarations
MSG msg;
//Check if a message is found
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
// Check for a quit message
if( msg.message == WM_QUIT ){
break;
}
TranslateMessage( &msg );
DispatchMessage( &msg );
}
//No message found? Start drawing
else
{
//Reset variable i
if(i==11)
i=0;
//Create the name of the file to be drawn
FileNameStream.str("");
FileNameStream << i;
FileName = "c:/temp/temp/hart_";
FileName += FileNameStream.str();
FileName += ".bmp";
//Envoke the drawing of the file
char* File = new char[FileName.size() + 1];
strcpy(File, FileName.c_str());
ProcessIdle(File);
delete File;
i++;
}
}
//Clean up the mess we made
CleanUp();
return 0;
}
//Als er niet gevraagd wordt om het bestand te beeindigen, teken dan naar het scherm
void ProcessIdle(char* File)
{
//Declarations
HRESULT hRet;
RECT rect;
static iLastBlit;
if(GetTickCount() - iLastBlit < 50)
{
return;
}
//Load the bitmap
BitmapSurface.LoadBitmap(hInst, File, ScreenHeight, ScreenWidth);
BitmapSurface.Draw(FrontBuffer);
//Get the screen size
ZeroMemory(&rect, sizeof( rect ));
GetClientRect(hMainWnd, &rect);
POINT p1;
POINT p2;
p1.x = rect.left;
p1.y = rect.top;
p2.x = rect.right;
p2.y = rect.bottom;
ClientToScreen(hMainWnd, &p1);
ClientToScreen(hMainWnd, &p2);
rect.left = p1.x;
rect.top = p1.y;
rect.right = p2.x;
rect.bottom = p2.y;
//Blit the image
while( 1 )
{
hRet = FrontBuffer->Blt(&rect, BackBuffer, NULL, DDBLT_WAIT, NULL);
if( hRet == DD_OK )
{
break;
}
if( hRet == DDERR_SURFACELOST )
{
FrontBuffer->Restore();
}
if( hRet != DDERR_WASSTILLDRAWING )
{
break;
}
}
iLastBlit = GetTickCount();
}
//A function to create the window used to view the video
HWND InitWindow(int iCmdShow)
{
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInst, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = TEXT("");
wc.lpszClassName = TEXT("Streaming DICOM - player");
RegisterClass(&wc);
hWnd = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
TEXT("Streaming DICOM - player"),
TEXT("Streaming DICOM - player"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
ScreenWidth,
ScreenHeight,
NULL,
NULL,
hInst,
NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
SetFocus(hWnd);
return hWnd;
}
//Is the window getting feedback from the keyboard? Respond here
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
//If escape is pressed exit the application
case WM_KEYDOWN:
if(wParam == VK_ESCAPE)
{
PostQuitMessage(0);
CleanUp();
return 0;
}
break;
//When the screen is destroyed exit the application
case WM_DESTROY:
PostQuitMessage(0);
CleanUp();
return 0;
}
//Geef een waarde terug
return DefWindowProc(hWnd, message, wParam, lParam);
}
//Create the DirectDraw enviroment
int InitDirectDraw()
{
DDSURFACEDESC2 ddsd;
HRESULT hRet;
// Create the main DirectDraw object.
hRet = DirectDrawCreateEx(NULL, (VOID**)&DD, IID_IDirectDraw7, NULL);
if( hRet != DD_OK )
return -1;
// Set the cooperativelevel
hRet = DD->SetCooperativeLevel(hMainWnd, DDSCL_NORMAL); //| DDSCL_MULTITHREADED);
if( hRet != DD_OK )
return -1;
// Prepare to create the primary surface by initializing the fields of a DDSURFACEDESC2 structure.
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
// Create the primary surface.
hRet = DD->CreateSurface(&ddsd, &FrontBuffer, NULL);
if( hRet != DD_OK )
return -1;
// Prepare to create the backbuffer surface by initializing the fields of a DDSURFACEDESC2 structure.
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = ScreenWidth;
ddsd.dwHeight = ScreenHeight;
// Create the backbuffer surface.
hRet = DD->CreateSurface(&ddsd, &BackBuffer, NULL);
if( hRet != DD_OK )
return -1;
//Create the clipper
hRet = DD->CreateClipper(0, &Clipper, NULL);
if( hRet != DD_OK )
return -1;
//Asign the clipper to the main window
hRet = Clipper->SetHWnd(0, hMainWnd);
if( hRet != DD_OK )
return -1;
//Asign the clipper to the front buffer
hRet = FrontBuffer->SetClipper(Clipper);
if( hRet != DD_OK )
return -1;
//End the function
return 0;
}
//This function cleanse up the mess which is made
void CleanUp()
{
//Destroy the Bitmap drawing surface
BitmapSurface.Destroy();
//Destroy the backbuffer
if(BackBuffer){
BackBuffer->Release();
BackBuffer=NULL;
}
//Destroy the front buffer
if(FrontBuffer){
//Destroy the clipper
FrontBuffer->SetClipper(NULL);
Clipper = NULL;
//Destroy the buffer
FrontBuffer->Release();
FrontBuffer=NULL;
}
//Destroy the DirectDraw Element
if(DD){
DD->Release();
DD=NULL;
}
}
cSurface.cpp:
///////////////////////////////////////////////////////////////////
// cSurface.cpp: implementation of the cSurface class. //
/////////////////////////////////////////////////////////////////
//Includes
#include "stdafx.h"
#include "cSurface.h"
// Constructors
cSurface::cSurface()
{
m_pSurface = NULL;
}
cSurface::cSurface(LPDIRECTDRAW7 hDD, HINSTANCE hInst, char nResource[], int nWidth, int nHeight)
{
m_pSurface = NULL;
Create(hDD, nWidth, nHeight);
this->LoadBitmap(hInst, nResource);
}
// Deconstructor
cSurface::~cSurface()
{
if(m_pSurface != NULL)
{
OutputDebugString("Surface Destroyed\n");
m_pSurface->Release();
m_pSurface = NULL;
}
}
//A function which loads a bitmap into the systems memory.
BOOL cSurface::LoadBitmap(HINSTANCE hInst, char nRes[], int nWidth, int nHeight)
{
//Declarations
HDC hdcImage;
HDC hdc;
BITMAP bm;
DDSURFACEDESC2 ddsd;
HRESULT hr;
int nX = 0;
int nY = 0;
HBITMAP hbm;
//Load the image into a variable
hbm = (HBITMAP) LoadImage(NULL, nRes, IMAGE_BITMAP, nWidth, nHeight, LR_LOADFROMFILE | LR_DEFAULTCOLOR);
if (hbm == NULL || m_pSurface == NULL)
return FALSE;
// Make sure this surface is restored.
m_pSurface->Restore();
// Select bitmap into a memoryDC so we can use it.
hdcImage = CreateCompatibleDC(NULL);
if (!hdcImage)
return FALSE;
SelectObject(hdcImage, hbm);
// Get size of the bitmap
GetObject(hbm, sizeof(bm), &bm);
if(nWidth == 0)
nWidth = bm.bmWidth;
if(nHeight == 0)
nHeight = bm.bmHeight;
// Get size of surface.
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
m_pSurface->GetSurfaceDesc(&ddsd);
//Resize the image to the size of the surface
if ((hr = m_pSurface->GetDC(&hdc)) == DD_OK)
{
StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, nX, nY,
nWidth, nHeight, SRCCOPY);
m_pSurface->ReleaseDC(hdc);
}
DeleteDC(hdcImage);
//Set variables to know stuff about the input file
m_srcInfo.m_hInstance = hInst;
m_srcInfo.m_nResource = nRes;
m_srcInfo.m_iX = nX;
m_srcInfo.m_iY = nY;
m_srcInfo.m_iWidth = nWidth;
m_srcInfo.m_iHeight = nHeight;
//End the function
return TRUE;
}
//A function which creates the surface on which to draw the bitmap
BOOL cSurface::Create(LPDIRECTDRAW7 hDD, int nWidth, int nHeight)
{
//Declarations
DDSURFACEDESC2 ddsd;
HRESULT hRet;
//Delcare the surface
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
ddsd.dwWidth = nWidth;
ddsd.dwHeight = nHeight;
//Create the surface
hRet = hDD->CreateSurface(&ddsd, &m_pSurface, NULL );
if( hRet != DD_OK )
{
//Is there a lack of video memort? Use the system memory
if(hRet == DDERR_OUTOFVIDEOMEMORY)
{
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
hRet = hDD->CreateSurface(&ddsd, &m_pSurface, NULL );
}
//Is there still a problem? End the program
if( hRet != DD_OK )
{
return FALSE;
}
}
//Set some variables
m_Width = nWidth;
m_Height = nHeight;
//End the function
return TRUE;
}
//Draw the image to the surface
BOOL cSurface::Draw(LPDIRECTDRAWSURFACE7 lpDest)
{
//Declarations
RECT rcRect;
HRESULT hRet;
int iDestX=(GetSystemMetrics(SM_CXSCREEN)-m_srcInfo.m_iWidth)/2;
int iDestY=(GetSystemMetrics(SM_CYSCREEN)-m_srcInfo.m_iHeight)/2;
int nWidth=m_srcInfo.m_iWidth;
int nHeight=m_srcInfo.m_iHeight;
//Set the rectangle
rcRect.left = 0;
rcRect.top = 0;
rcRect.right = nWidth;
rcRect.bottom = nHeight;
//Run this loop while drawing
while(1)
{
//Blit the image to the surface
hRet = lpDest->Blt(&rcRect, m_pSurface, NULL, DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT, NULL);
//Error checking
if(hRet == DD_OK)
break;
if(hRet == DDERR_SURFACELOST)
{
Restore();
}
else
{
if(hRet != DDERR_WASSTILLDRAWING)
return FALSE;
}
}
//End the function
return TRUE;
}
//A function which destorys the stuff we made
void cSurface::Destroy()
{
if(m_pSurface != NULL)
{
m_pSurface->Release();
m_pSurface = NULL;
}
}
//Get the height of the local surface
UINT cSurface::Height()
{
return m_Height;
}
//Get the width of the local surface
UINT cSurface::Width()
{
return m_Width;
}
//Get the local surface
LPDIRECTDRAWSURFACE7 cSurface::GetSurface()
{
return m_pSurface;
}
//Restore the local surface
void cSurface::Restore()
{
m_pSurface->Restore();
}
cSurface.h
//////////////////////////////////////////////////////////////////
// cSurface.h: interface for the cSurface class. //
////////////////////////////////////////////////////////////////
#if !defined(AFX_CSURFACE_H__0C439CBB_20C0_487F_84A6_31D8F436B74A__INCLUDED_)
#define AFX_CSURFACE_H__0C439CBB_20C0_487F_84A6_31D8F436B74A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif
#include <ddraw.h>//Create a structure that defines the information of the video source
struct SURFACE_SOURCE_INFO
{
HINSTANCE m_hInstance;
char *m_nResource;
int m_iX;
int m_iY;
int m_iWidth;
int m_iHeight;
};
//Create variables and prototypes
class cSurface
{
public:
SURFACE_SOURCE_INFO m_srcInfo;
void Restore();
LPDIRECTDRAWSURFACE7 GetSurface();
UINT Width();
UINT Height();
void Destroy();
BOOL Draw(LPDIRECTDRAWSURFACE7 lpDest);
BOOL Create(LPDIRECTDRAW7 hDD, int nWidth, int nHeight);
BOOL LoadBitmap(HINSTANCE hInst, char nRes[], int nWidth = 0, int nHeight = 0);
cSurface(LPDIRECTDRAW7 hDD, HINSTANCE hInst, char nResource[], int nWidth, int nHeight);
cSurface();
virtual ~cSurface();
protected:
UINT m_Height;
UINT m_Width;
LPDIRECTDRAWSURFACE7 m_pSurface;
};
#endif
StdAfx.cpp
// stdafx.cpp : source file that includes just the standard includes
// BasicDD.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
StdAfx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
Can anyone help?
|
|
|
|
|
I need to create a modeless dialog but when I use Create() the dialog quickly appears and disappears.
As an expirement I tried cerating a quick dialog-based app using the C++ wizard and created a button that brings up the about box (as created by the wizard) without using DoModal.
Does anyone know how to do this please? It's quite frustrating.
|
|
|
|
|
If you could post the code how you are creating the modeless dialog box may be that would pin point the prob.
any ways here how i would create the modeless dialog.
CMydialog *pDialog;
pDialog = new CMydialog;
pDialog->Create(CMyDialog::IDD);
pDialog->ShowWindow(SW_SHOW);
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|
Your code works fine. It was exactly the same as mine only you used a pointer to a dialog and I used an instance directly. Doesn't really make much sense to me but as long as it works, I'm happy right now
Thanks for your help.
|
|
|
|
|
But do remember that the dialog object is created using new so there should be a corresponding delete as well.
usally the modeless dialogs are self deleteing,
you put
delete this
in OnDestroy event.
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|
Mr.Prakash wrote:
...in OnDestroy event.
Actually, the this pointer should be deleted in PostNcDestroy() .
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
yeah correct.
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|