locked
Error Occuring in C++ I simply cannot understand or figure how to programatically fix. Help Please :) RRS feed

  • Question

  • I would have put a better title to this thread, but in all honesty, I have no idea what to call the thread to get the right people to read it.

    I have been learning C++ while making my FFP_LockScreen 2.0, which has proven to create a great challenge to making the project work all the time.

    I am guessing my major issue in the program right now is probably Memory Allocation [something I haven't done much of in the program], but what is really puzzling me is why adding a couple lines of code to a certain place 'breaks' the program.

    I am resilient to post code for FFP_LS 2.0 here, but i will put up snippets that I feel concern the issues.

    In the WM_CREATE message of my program (It is Win32, not MFC or ATL) I call a method "LoadImages()" which I created. The Method starts with some Registry reading, then closes the key, then based on what it read it creates some wchar_t variables for the program. These are, in turn, the paths to all the images to load.

    Images that do not require to be stored in memory (not re-drawn often) have their paths stored, while images that do need to be redrawn often are drawn to a HDC in memory.

    Code Sample for images whose paths are stored:

    wcscpy((WCHAR *)&battery_full, (WCHAR *)&longPath);
    wcscat((WCHAR *)&battery_full, L"\\battery_full.png");
    

    Where longPath is generated with those Registry reads, and battery_full is a global variable storing the path. This works just fine.

    Code Sample for images who are drawn often:

    wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    wcscat((WCHAR*)&temps, L"\\touch_zone.png");
    
    if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
            if (FALSE == DrawAlphaImage(&hdcMem[HDC_TOUCH_ZONE], pImage, &touchZoneRC))
            {/*Err*/}
    memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    

    Where temps is a wchar_t that temporarily holds the path to the image being loaded, and hdcMem is an array of HDCs. There is no point where I am accessing parts of the array beyond the limit of the array, so that is not the issue.

    Code for DrawAlphaImage()

    BOOL DrawAlphaImage(HDC *hDC, IImage *pImage, RECT *prcBounds)
    {
      //Make the HDC
      ImageInfo II;
      pImage->GetImageInfo(&II);
    
      *hDC = CreateCompatibleDC(NULL);
      SelectObject(*hDC, CreateBitmap(scrSZ.cx, scrSZ.cy, 1, 32, NULL));
      SetBkMode(*hDC, TRANSPARENT);
    
      if (useBGColor)
      {
        FillRect(*hDC, prcBounds, CreateSolidBrush(bgColor));
      }
      else
      {
        BitBlt(*hDC, prcBounds->left, prcBounds->top, prcBounds->right - prcBounds->left, prcBounds->bottom - prcBounds->top, hdcMem[HDC_BG], prcBounds->left, prcBounds->top, SRCCOPY);
      }
    
      HRESULT hr = pImage->Draw(*hDC, prcBounds, NULL);
      
      pImage->Release();
    
    
      return (hr == S_OK);
    
    }
    

    So my main issue is that I recently added a little bit of code into the program, which WAS working, and now it only works in the case that I start the program in Debug mode (hooked up with Visual Studio) and I make a breakpoint at the 6th Image load, where the image is being loaded into memory. At first I thought it pertained to a specific line of code, but it is actually just that specific place in code, relatively.

     

    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\touch_zone.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_TOUCH_ZONE], pImage, &touchZoneRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\status_inactive.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_STAT_INACTIVE], pImage, &statusTextRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\status_scanning.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_STAT_SCANNING], pImage, &statusTextRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\status_failure.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_STAT_FAILURE], pImage, &statusTextRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\status_success.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_STAT_SUCCESS], pImage, &statusTextRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			//ScanBar (BMP)
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\scanbar.bmp");
    /*BREAKPOINT*/		if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage)) //Error on THIS line..... -_-
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_SCAN_BAR], pImage, &origScanBarRC))	
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\signal_empty.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_SIG_NONE], pImage, &signalRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\signal_one.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_SIG_ONE], pImage, &signalRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\signal_two.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_SIG_TWO], pImage, &signalRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\signal_three.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_SIG_THREE], pImage, &signalRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\signal_four.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage))
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_SIG_FOUR], pImage, &signalRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    
    			wcscpy((WCHAR*)&temps, (WCHAR*)&longPath);
    			wcscat((WCHAR*)&temps, L"\\signal_full.png");
    			if (S_OK == pFactory->CreateImageFromFile((WCHAR *)temps, &pImage)) 
    				if (FALSE == DrawAlphaImage(&hdcMem[HDC_SIG_FULL], pImage, &signalRC))
    				{/*Err*/}
    			memset(&temps, 0, sizeof(CHAR) * MAX_PATH);
    

    That code is the bulk of the LoadImages function. Where i have "/*BREAKPOINT*/" Is where I MUST place a breakpoint when debugging in order to make the program work. If I don't place it there, then the program doesn't load (It loads partially, but never successfully Paints). I have no idea WHY this error is occuring, but I have been thinking it may have to do with a memory leak somewhere, or maybe I have some Asynchronous calls going on, that I haven't been paying attention to. Either way, I find it very odd that no matter how I seem to re-arrange the code, it is always the 6th call of pFactory->CreateImageFromFile() that requires a break-point in order to function.

    Thinking that it may have to do with timing, i have tried placing "Sleep(100)" there, but it doesn't work.

    I know the program hasn't frozen, because I have a timer in there that after 2 Minutes kills the app [the SetTimer() function comes after the LoadImages() call] so I know it is still processing, it is just screwing up right there...

    If you know C++ pretty well, and want to help me out, but feel you need to see my whole code, contact me and we will figure something out because I do really need this fixed up

    Thank You :)

    Tuesday, August 10, 2010 3:02 AM

Answers

  • How do you declare temps ?

    WCHAR temps;
    or
    WCHAR temps[256];
    or
    WCHAR* temps;
    or
    others?


    - afriza
    Wednesday, August 11, 2010 6:40 AM

All replies

  • How do you declare temps ?

    WCHAR temps;
    or
    WCHAR temps[256];
    or
    WCHAR* temps;
    or
    others?


    - afriza
    Wednesday, August 11, 2010 6:40 AM
  • I am not that great a C++ dev so don't laugh at me for doing this.  I must have had some reason for doing this when I started.

     

    I declar temps as

     

    CHAR temps[MAX_PATH];

     

    Why it isn't WCHAR I am not entirely sure, but I think I had originally started with WCHAR, and ran into a few errors there [probably my own mistakes] and switched to CHAR.

     

    In any case, on the XDA Forums, i posted this question as well, and some of the devs over there think it may be a Threading issue.  When I run the LoadImages method, it creates another thread to initialize the pFactory.  So some devs over there think it may have something to do with Timing between the two threads.  But, I am really a noob to C++ (this is literally my first project) and I have no idea about Thread timing and management.

     

    (Link to XDA Forums thread: http://forum.xda-developers.com/showthread.php?p=7624330 )

     

     

    EDIT:

     

    Thank You so much Afriza N. Arief!  I fixed up those stupid declarations of all the paths as CHAR instead of WCHAR, and it works now :)  You rock :D

    Thursday, August 12, 2010 1:23 PM
  • Wait, your post here for the solution contradicts the one at XDA-developers, so you change all to WCHAR instead of CHAR, am I right?
    - afriza
    Wednesday, September 15, 2010 1:26 AM