香港新浪網 MySinaBlog 精選話題工具
Chi Lap | 28th Apr 2012, 01:31 | C/C++ | (1 Reads)

Yesterday, I modified some C++ code and faced a quite weird problem. Long story short, I changed the code that declares an instance of a class from something like

/* declares 'a' to be an instance of 'ObjectA' and
the constructor takes a pointer to a variable as the parameter
  */
ObjectA a(&hello);   
/* no problem here */
a.DoSomething();     

to

/* similar to the above but the constructor now takes an reference of ObjectB as the parameter and ObjectB's constructor in turn takes a pointer to 'hello' as the parameter */
ObjectA a(ObjectB(&hello)); 
/* By this way of declaration, this line cause C2228 error, i.e. 'a' is not a class or struct! Why ? */
a.DoSomething();    

I tried to compile the same code with g++. Surprisingly, a similar error was reported! What's wrong ? Both compilers didn't complain on the declaration line, which means the syntax is correct. However, base on the error reported, although the 'declaration' was syntactically correct, both compilers couldn't (or didn't) determine that I was declaring a variable 'a'!

 (閱讀全文)

Chi Lap | 22nd Mar 2012, 15:39 | Android | (87 Reads)

There is a recommended way to build native C/C++ program on Android: List your source files in a script (Android.mk) and run 'ndk-build'. I have also outlined the steps described in NDK development guide at
http://clseto.mysinablog.com/index.php?op=ViewArticle&articleId=3226883

It may not be a problem if you are writing new programs but if you already have a working build script (like those open source softwares) which can build you program nicely on Linux, it would be a headache to migrate your script to Android.mk. All you need in this situation is a C/C++ cross compiler and then replace the variables in your script (such as CC, CXX, AR, AS, RANLIB, ...) to
something like 'arm-linux-androideabi-gcc', 'arm-linux-androideabi-g++', ...

Fortunatley, inside the 'Android NDK Dev Guide', there is a section 'Standalone Toolchain' which just describes what we need. You can download the latest NDK at http://developer.android.com/sdk/ndk/index.html. I tried the r7b version of NDK on Linux to perform the test.

 (閱讀全文)

Chi Lap | 22nd Mar 2012, 10:59 | Windows programming | (3 Reads)

When exporting a C function in dll using VC++, one usually adds a macro like:

#ifdef LIBDLL_EXPORTS
#   define _DLLAPI __declspec(dllexport)
#else
#   define _DLLAPI __declspec(dllimport)
#endif

and then placed the macro '_DLLAPI' in front of a function in a header like:

_DLLAPI const int GetSomething(int v);

There is no problem until

  1. you are declaring a template function in a header and
  2. there is one implementation of the template in a dll and
  3. there is another implementation in your final exe

Just like the picture below:

Picture

Both C files (one in dll and one in exe) include the template header file.

When you compile it with VC++, a warning is printed:

xxxx.obj : warning LNK4217: locally defined symbol xxxxxxxxxxxxxxx

This warning complains that you have an implementation of a function inside an exe (locally defined symbol) but is declared as '__declspec(dllimport)'. This occurs because I have 2 different implementations of the template function: one in my exe and another one in the dll. I want to call the one in dll from my exe, thus, it is normal to add '_DLLAPI' in front of the declaration (which will be expanded to __declspec(dllimport) ) but that declaration conflicts with the one in my exe!
If I take away '_DLLAPI' from the header, the compiler then complains the declaration of the header does not match that inside the dll souce. So what can I do?

 (閱讀全文)

Chi Lap | 20th Dec 2011, 15:35 | Building software | (19 Reads)
For quite a long time, I don't have the chance to compile free software from scratch. Yesterday, I need to cross compile (host platform is a normal x86 desktop and target platform is a MIPS embedded system) the open source libcurl library with ssl support. It seems not difficult at first sight but it finally wasted me a couple of hours to do so ... (閱讀全文)

Chi Lap | 17th Dec 2011, 21:39 | C/C++ | (43 Reads)

Openssl 1.0.0e library leaks memory if you have called 'SSL_library_init()' without calling clean up functions. Thus, I make a 'EVP_cleanup()' at the end of program:

#include "stdafx.h"
#include <windows.h>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/rand.h"
#include "openssl/ssl.h"
#include "openssl/x509v3.h"

int _tmain(int argc, _TCHAR* argv[])
{
    SSL_library_init();  // init Openssl

    EVP_cleanup();       // clean up

    _CrtDumpMemoryLeaks();  // dump any memory leak
   
return 0;
}

The 'crtdbg.h" and '_CrtDumpMemoryLeaks()' are Visual C++ specific functions for detecting memory leaks. Nothing is done between 'SSL_library_init()' and 'EVP_cleanup()' to ensure that no other stuff affect these 2 functions. I originally think everything is OK after calling 'EVP_cleanup' but there are still 2 blocks of memory leaked as reported by _CrtDumpMemoryLeaks():

Detected memory leaks!
Dumping objects ->
{223} normal block at 0x009E7E40, 16 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
{222} normal block at 0x009E7D58, 20 bytes long.
 Data: <    @~          > 00 00 00 00 40 7E 9E 00 00 00 00 00 04 00 00 00

Therefore, I try to add these cleaning functions after 'EVP_cleanup' one by one:
ERR_remove_state(0);
COMP_zlib_cleanup();
OBJ_NAME_cleanup(-1);
CRYPTO_cleanup_all_ex_data();

However, they can't wipe out the leaked blocks ...

 (閱讀全文)

Chi Lap | 28th Nov 2011, 12:09 | misc | (34 Reads)

The Android market automatically updated recently and connects much slower than before when connected with WiFi. Besides, most of the download actions stuck at the "Downloading" progress bar without even 1 byte of data downloaded. I wondered what happened to the new Android Market ? When I switched to use 3G network, the Market behaved nicely without blocking any more. There should be something wrong with the WiFi connection of my home router.

Actually, I don't know what's wrong with my router. I can access all web pages with it without problem except the Android Market. Eventually, I had to Google the problem on internet and found someone said it was something wrong with the DNS server settings of your WiFi connection.

 (閱讀全文)

Chi Lap | 14th Nov 2011, 11:50 | Android | (23 Reads)

Uploaded a little app, "Phone 2 Mouse", to Android market last night.

Picture

It is an app which turns your Android phone to a wireless mouse so as to control the mouse pointer on your Windows PC (in addition, you need to run a small program on your Windows PC, too, so that this Android app can take effect). For details, please visit http://clseto.sinaman.com

One can download "Phone 2 Mouse" at market://details?id=com.sss.p2mclient or use a QR code reader on your phone:

Picture

 

 (閱讀全文)

Chi Lap | 2nd Nov 2011, 15:18 | Network | (14 Reads)

Nowadays, most online apps are web apps, which means they are based on HTTP + XML or HTTP + JSON. Gradually, less application level protocols are invented these days and so less applications directly work on TCP/UDP sockets.

Recently, I was working on a hobby project which requires communication between an Android device and a Windows desktop machine. HTTP is a somewhat, I think, too expensive and redundant protocol for my hobby app, since the data passed between the 2 machines are of small chunks and need to be fast (the "data" itself are actually some mouse clicks and mouse displacement
values).

I am a person who would like to do things from scratch. Thus, I have invented a very simple protocol to suit the needs of my app. Eventually, I came to a problem that most people would encounter when developing programs which require remote connection -- how to detect the peer still exists after a TCP 3 way handshaking is completed ?

 (閱讀全文)

Chi Lap | 15th Oct 2011, 15:08 | misc | (6 Reads)

Supposedly, there is already a working copy of project A from one svn repository and I would like to include source from project B in a different svn repository:

projA
  |
  +---- srcA (existing working copy of projA)
  |
  +---- projB (want to add reference to projB)
          |
          +
- - - > (external) - - -> projB
 

Why do I want to do this ? Because project B is just a general library project (can be used by any other projects) and project A is a 'exe' project which make use of the functions in project B. The goal here is whenever someone wants to check out source from project A, the source of project B will be checked out automatically (since project A won't compile if project B is missing).

Let's outline the steps below:

 (閱讀全文)

Chi Lap | 13th Oct 2011, 14:48 | Java | (38 Reads)

Yesterday, I wrote a testing program on Android to try fetching data from a https server. Out of my expectation, I got this error, actually an exception, from the apache http client:

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

 (閱讀全文)

Next