title: Android native process: stack corruption detected
body: <p>I'm enhancing a native app which is already part of Google source. I'm seeing a crash. I have tried debugging this, but couldn't conclude. Your help is greatly appreciated:</p>
  
  <pre><code>struct device_global {
      struct support *sport;
      struct support_params params;
      struct global_priv *ctrl;
  
  #if defined FEATURE_1
      int freq, freq_2;
  #endif /* FEATURE_1 */
  
  #ifdef FEATURE_2
      int wifi_display;
      #define SUBELEMS 10
      struct buf *subelem[MAX_SUBELEMS];
  #endif /* FEATURE_2 */
  
      struct list_entry *add_list_entry;
  
  #ifdef FEATURE_3
      void* my_context;
  #endif /* FEATURE_3 */
  };
  
  typedef unsigned long       DWORD;
  typedef DWORD           *PDWORD;
  
  typedef struct
  {
      DWORD dwFlags;
      DWORD dwErrorCode;
      DWORD dwDeviceId;
  
  #ifdef FEATURE_X
      CHAR* tableFileName;
  #endif
  
  #ifdef FEATURE_Y
      FILE* tableFile;
      DWORD headerVersion;
      DWORD headerSize;
  #endif
  } CONTEXT1, *CONTEXT2;
  
  
  struct device_global * init(struct support_params *params)
  {
      struct device_global *global;
      global = os_malloc(sizeof(*global));
      if (params-&gt;ctrl)
              global-&gt;params.ctrl =  os_strdup(params-&gt;ctrl);
      // Assignment of other global variables done here like above (not added here to remove clutter)
  
      int deviceId = 0;
      if (0 == getDeviceId(global-&gt;my_context, (PDWORD) &amp;deviceId))
      {
          printf("Device ID 0x%x", deviceId);
      }
      printf("Before returning global");    // gets printed before crash
      return global;   // crashes here
  }
  
  
  
  DWORD getDeviceId(PVOID pContext, PDWORD myDeviceId)
  {
      CONTEXT2 myContext;
  
      if (!pContext || !myDeviceId)
      {
          return -1;
      }
      else
      {
          myContext = (CONTEXT2) pContext;
          *myDeviceId = myContext-&gt;dwDeviceId;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The crash is happening exactly in init method at "return global". The printf statement gets printed and the crash appears after that.
  Kindly share your valuable input.</p>
  
  <p>The error message corresponding to the crash is:</p>
  
  <pre><code>03-16 12:30:03.230  5626  5626 F DEBUG   : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
  03-16 12:30:03.232  5626  5626 F DEBUG   : Abort message: 'stack corruption detected'
  </code></pre>