If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

If someone can tell me why my program crash

Hello
I am trying to connect MySQL database from C++ code given below-
My development environment is -
Window 7
CODE::BLOCKS 10.05 with minGW mingw32-g++.exe
MySQL Connector C++ 1.0.5\lib\debug\mysqlcppconn.lib
Up to compilation and linking every thing is OK I followed the tutorial http://dev.mysql.com/doc/refman/5.1/...example-1.html

When I run the code it shows
SELECT 'Hello World!
SELECT 'Hello World!....1
SELECT 'Hello World!....2
And then Debug window open with a message "bill.exe has stopped working", I copied libmysql.dll to my application debug folder.
My assumption is that line no. 31 is raising problem because after that cout for Hello World!...3 is not executing.

May be require some port or network related file.
Any idea or advice will be great help for me.
Thank you

Re: If someone can tell me why my program crash

Originally Posted by vishalon

Hello
I am trying to connect MySQL database from C++ code given below-
My development environment is -
Window 7
CODE::BLOCKS 10.05 with minGW mingw32-g++.exe
MySQL Connector C++ 1.0.5\lib\debug\mysqlcppconn.lib
Up to compilation and linking every thing is OK I followed the tutorial http://dev.mysql.com/doc/refman/5.1/...example-1.html

Honestly, that is a poorly coded example. There is no check for errors anywhere, no check for NULL, nothing. Then you have this:

Code:

delete res;
delete stmt;
delete con;

Are you sure that the memory here was allocated with new? Even if it is, your code has a memory leak if it throws an exception after any of those items are allocated. The catch block doesn't remove any allocated resources. So the code has all sorts of problems if anything happens to fail.

I have never used this particular MySQL library, but here is a much cleaner implementation of that example program:

Note that there is now a check for NULL, and that the resource is cleaned up automatically, even if the catch() block is executed. The reason why you don't need "delete res", "delete stmt", etc. anymore is the usage of that tiny SQLResource() class that I defined at the beginning of the code. That code does all of this magic, since the destructor for the object cleans up the resource that was assigned to the class.

Basically, what I wrote could have been done using a smart pointer (std::shared_ptr), but just in case you're using VC++ that doesn't have shared_ptr, the code above will suffice for now. The bottom line is that the memory leak that would have happened is now taken care of.

I didn't compile this code, but if it compiles, then you have to debug it to make sure where things go wrong (if they go wrong).

Re: If someone can tell me why my program crash

Dear Paul McKenzie thank you for considering and reply. I am not friendly with C++, I'm learning C++
While compiling the code,compiler is complaining

C:\msqldb\bill\main.cpp: In constructor 'SQLResource<Res>::SQLResource(Res*)':
C:\msqldb\bill\main.cpp:21: error: class 'SQLResource<Res>' does not have any field named 'm_pRes'
C:\msqldb\bill\main.cpp: In destructor 'SQLResource<Res>::~SQLResource()':
C:\msqldb\bill\main.cpp:22: error: 'm_pRes' was not declared in this scope

Re: If someone can tell me why my program crash

Dear Paul McKenzie thank you for considering and reply. I am not friendly with C++, I'm learning C++
While compiling the code,compiler is complaining

See my edited post. Secondly, if you're just learning C++, why are you using a third-party library to learn from? You should be learning from books, tutorials, etc.

The MySQL library assumes you know C++ already, so when you use the library, you know exactly what you're doing. Otherwise, all you will be doing is writing apps that are full of mistakes, memory leaks, etc.

Re: If someone can tell me why my program crash

Originally Posted by Paul McKenzie

See my edited post. Secondly, if you're just learning C++, why are you using a third-party library to learn from? You should be learning from books, tutorials, etc.

The MySQL library assumes you know C++ already, so when you use the library, you know exactly what you're doing. Otherwise, all you will be doing is writing apps that are full of mistakes, memory leaks, etc.

Regards,

Paul McKenzie

OK Paul can you give some guidance where I can get good tutorial on the topic I am looking for that is Using MySQL database from C++