|
I see what you mean but how can something like this crash on "new ..."?
char* cNewString = NULL;
// cString is not empty
cNewString = new char[strlen(cString) + 1];
-Tom
|
|
|
|
|
Visual C++ is not perfect in reporting the cause of a crash and where the bug resides. One possible cause of delete [] is that the program passes the dynamically allocated array into a function that does not release the memory even after the function has returned.
Kuphryn
|
|
|
|
|
Probably a newbie question but I really want to ask, I have like hundreds of jpg's, gif's and icons need to be shared by multiple application, I heard we can encapsulate all of them into a dll and use that dll to export resource ID's when required by application, but how? Could someone please tell me:
1, what kind of dll I will need to create? Win32 dll? MFC dll? Or others?
2, How do I export resource ID's from a dll?
Thanks a lot.
|
|
|
|
|
putting hundreds of jpgs in a dll is a very bad idea, because it will waste your memory... you won't need them all the same time!
create a folder, put them in there, then create a new dll with 2 exported function: LoadJpg and FreeJpg!
LoadJpg allocates memory and loads the file from disk, FreeJpg frees the memory allocated by LoadJpg!
This is the best way!
Don't try it, just do it!
|
|
|
|
|
Yeah I thought so, but I was just doing some practice, thanks for the suggestion though...
Anyway, I think I have found out the solution already, I created a DLL and included images into that DLL as its resources, then in my executable I read images("resource blocks") from the DLL into my executable process's memory blocks when needed, then draw them on my executable's windows, tested and worked.
|
|
|
|
|
use a normal win32 dll and include the resources there!
then load this dll via LoadLibrary!
now, you can open a bitmap for example by calling LoadBitmap with hInstance of the loaded dll!
Don't try it, just do it!
|
|
|
|
|
I am trying to create an application that is capable of working with multiple document types, each having its own view. In the InitInstance() function I have done the following:
BOOL CTestApp::InitInstance()
{
... Other code
LoadStdProfileSettings(15); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_FIRSTTYPE,
RUNTIME_CLASS(CTypeOneDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CTypeOneView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
CMultiDocTemplate* pDocTemplateT;
pDocTemplateT = new CMultiDocTemplate(IDR_SECTYPE,
RUNTIME_CLASS(CTypeTwoDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CTypeTwoView));
if (!pDocTemplateT)
return FALSE;
AddDocTemplate(pDocTemplateT);
... Other code
I have all appropriate view and document classes in place for these doc templates and the project compiles without any problems. The first doc view is derived from CHTMLView and the second is derived from CRichEditView.
PROBLEM:
I create a new rich edit document, type some text into the view, and save it and then close the document. Then when I open the document I just saved, it appears that the document's contents had never been saved, or if they had, they certainly didn't open properly.
ALSO:
The messagebox that usually automatically pops up when you try to close a doc that has not been saved since its last modification fails to come up like it should.
I believe that these two problems are somehow related. Please help.
This has really got me stuck. Does anyone have any suggestions? Any leads to the problem would definitely be appreciated.
BNEACETP
|
|
|
|
|
|
Thanks for letting me know about the book.
I just ordered it earlier today.
BNEACETP
|
|
|
|
|
I was wondering if it were at all possible to derive a class template from another class template and keep the base class templates arguments abstract. I have looked around and cannot find the answer.
|
|
|
|
|
Mike Roode wrote:
I was wondering if it were at all possible to derive a class template from another class template
template<class T>
class Base
{
};
template<class T>
class Derived : public Base<T>
{
};
Is this what you meant? You can add any additional template parameters required in derived.
If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts you aim;
Yours is the Earth and everything that's in it.
Rudyard Kipling
|
|
|
|
|
That is what I was looking for. Thanks for the syntax Andrew.
|
|
|
|
|
Hi everyone. Again I have some questions, actually a problem with using the MFC class CRecordset. I have implemented a class which will manipulate a database (Oracle 9i). When I try to add a new employee (Employee is the object that carries the data) I get the following error:
---------------------------------------------------------------------------------------
Runtime error:
The application has requested the Runtime to terminate it in an unusual way.
I have this error only with the specific method. The other operations (update, select, delete on the same table work fine).
Does anyone know what is causing it?
bool Service::AddNew(Employee e){
long id;
EmployeeRecordset records(db);
//Begin transaction
db->BeginTrans();
//open the recordset only for adding records mode
try{
if ((records.Open(CRecordset::dynaset,NULL,CRecordset::appendOnly))!=0){
//if recordset is updateable add new record
if (records.CanAppend()){
//find the last YP_ID
records.MoveLast();
id = records.m_YP_ID + 1;
records.AddNew();
records.SetFieldNull( NULL );
//copy new values
//this where I copy the new values to the recordset
//here is where I have the error
if (records.Update()){
//end transaction
db->CommitTrans();
records.Close();
return (true);
}//end if
}//end if
}//end if
}//end of try
catch (CDBException cdbe){
db->Rollback();
records.Close();
return (false);
}//end of catch
catch (CMemoryException cme){
db->Rollback();
records.Close();
return (false);
}//end of catch
cout << "end of addnew" << endl;
db->Rollback();
records.Close();
return(false);
}//end of AddNew
|
|
|
|
|
I'm making a server that should be able to handle multiple connections at once. I use WSABUF, WSASend and WSAReceive in my code but when i try to compile i get the error that they are undefined, even though i am sure winsock 2 is included because i have explicitly included winsock2.h but also have defined _WIN32_WINNT as 0x400. So does anyone know why it is giving me the errors? do i need to include something else as well?
And another question, i am making a server that will be handling upto about a maximum of 200 clients. I've looked into some ways to do it and as i see it there are two ways: A simple multi-threaded server, or to use Input Output Completion Ports. I was told the latter was faster and more stable but i was wondering, where do u draw the line to go from a simple multi-threaded server to a IOCP server?
Many thanks in advance.
Kuniva
--------------------------------------------
|
|
|
|
|
Include ws2tcpip.h
I highly recommend IOCP for multiclient servers. There are several factors to consider when deciding on IOCP or multithreading such as OVERLAPPED and event driven I/O. One is connection. Given that 100 clients connect in within 1 sec, IOCP surpasses all other nonblocking I/O. Overall, the benefit of I/O is in the handling of thousands of clients with limited or any lag or loss of data.
Kuphryn
|
|
|
|
|
Cool, thanks!
Kuniva
--------------------------------------------
|
|
|
|
|
ack.. now i have another problem, it still seems to include winsock.h somehow because when i try to compile it gives me a lot of redefinition errors between winsock.h and WS2tcpip.h (like IP_TOS, IP_TTL etc).
Kuniva
--------------------------------------------
|
|
|
|
|
Include winsock2.
Kuphryn
|
|
|
|
|
Hi,
I'm writing an application that stores some temporary files on disk and deletes them on closing (there is no way around this). To be tidy, I want to tell the application to delete the files if the computer is restarted whilst it is running, and thus I have used MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT for Win2000 etc, and the WinInit.ini file for Windows 9x.
When the program ends, it is easy to cancel the "DeleteFileOnReboot" function I am using for Win 9x, as all I have to do before amending WinInit.ini is check if it exists (if it doesn't I can just create it then delete it on closing), and if it does save its settings using GetPrivateProfileSection and then restore them when my app ends using WritePrivateProfileSection.
However, I have no idea how to cancel MoveFileEx for Win2000/NT/XP platforms, as this stores all the files to be deleted in the registry. (I am a relative novice, and have never played with the registry before in an application.)
MSDN gives the following info:
[quote]
The function stores the locations of the files to be renamed at restart in the following registry value:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations
This registry value is of type REG_MULTI_SZ. Each rename operation stores the following pair of NULL-terminated strings:
szDstFile\0\0
szSrcFile\0szDstFile\0\0
[/quote]
Unfortunately, I have no idea how to use this information... If anybody could tell me how I can cancel MoveFileEx after it has been called for files using the MOVEFILE_DELAY_UNTIL_REBOOT flag, I would be really grateful.
Many thanks,
KB
P.S. I am using the straight Windows API with *no* MFC.
|
|
|
|
|
IMO you're approaching the problem the wrong way. Instead of complicated logic involving INI files and cancelling API calls, just keep a list of the temp files you create. In your app's shutdown code, go thru the list and delete the files. You can handle WM_QUERYENDSESSION and WM_ENDSESSION to know when the computer is being shut down.
--Mike--
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--=
NO CARRIER
|
|
|
|
|
Thank you for your reply, much appreciated - this is really useful information. I didn't even know about these messages (my knowledge is fairly limited as I'm in the process of learning).
Unfortunately this isn't really a solution, though, from what I have read on MSDN. If a user hits the "power off" button or the "reboot" button on his (or her) PC, the WM_QUERYENDSESSION and WM_ENDSESSION messages won't get sent, meaning the files will be left on the harddrive. So unless I am totally wrong about this, I guess I am stuck with trying to find out how to delete the relevant MoveFileEx info from the registry...
Many thanks for any help,
Cheers,
KB
|
|
|
|
|
Think of it this way: how often is the user going to turn off the power? How often is he going to shut your program down normally? How often will he log off/restart with your app open? I'd wager 1 power-off for 1000 normal shutdowns. Handling that 1 power-off is making your code much more complicated and harder to maintain, and as you've seen, you might not even be able to handle your clean-up properly. Why break your code for that 1 case? If the user cuts power, tough for him, other programs aren't going to handle it right either.
--Mike--
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--=
NO CARRIER
|
|
|
|
|
Hi, thanks again for your reply, I'm grateful for your suggestions.
You are of course right, but the problem is that some of the (less responsible) users I am writing this program for *are* likely to reboot mid-program - with the intention of accessing the temporary files. I'm actually writing a freeware 3rd party packer and launcher for a "hobbyist" game engine. Because it is third party, it needs temporarily to write a few of the game files to disk (nothing can be done about that, short of writing a whole virtual file system which is way beyond my ability). The main concern of some of the users who will use this program to pack and launch their own games will be how safe their temporary files are (as these temporary files may be original models or even game scripts that have taken a lot of work). To facilitate this I have added about as much protection as is reasonable for files that have to be temporarily on the hard drive - the files are deleted if the program is exited or if the system is rebooted, and whilst the game is in action the user can be prevented from alt-tabbing to Windows to find the files (the last part is optional - most people, myself included, would not want to use a program that prevents the switching to Windows whilst in use, but I've included the option for those developers who want to release demos to other users of the engine whilst the game is in development, so that they can prevent others ripping their files).
So essentially, it's a security issue as opposed to a "cleanliness" issue, which is why I want to include the remove-on-reboot code but also cancel it if it proves unnecessary. I hope this makes sense. I'm aware that what I want to do isn't ideal, but it's kind of necessary.
Again, many thanks, and any further help that anyone can give would be much appreciated.
Cheers,
KB
|
|
|
|
|
Why not use memory files. I don't know if you are using MFC, but have a look at the CMemFile class. I would think other class libraies would have something similiar.
Sonork 100.11743 Chicken Little
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
Within you lies the power for good - Use it!
|
|
|
|
|
Many thanks for your reply. Frankly, I would love to use memory files and avoid writing the data to disk at all. Unfortunately, I don't think it's practical - I wish it was. The problem is this:
My app is essentially a launcher for the game executable. The game executable and some of its files (models, scripts etc) are kept in an archive. My launcher extracts this archive to a temporary location on disk and runs the game from there. Would it be possible to extract these files to memory and run them from there? If so, how would I go about it? And would they be able to access other files on the hard-disk? A bigger problem is that using memory files would consume RAM that the game needs. As I have no way of accessing the file loading code of the game engine (it has an SDK but it is fairly limited in that respect, mainly aimed at adding graphics capabilities), I cannot tell the game executable to load files into memory, open them, then destroy them...
If anyone can think of a better solution than the one I am currently using, I would be really grateful... If not, I'd still be grateful for some help with cancelling MoveFileEx!
Many thanks, all help much appreciated,
KB
|
|
|
|
|